main.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  3. void main() => runApp(new MyApp());
  4. class CustomPicker extends CommonPickerModel {
  5. String digits(int value, int length) {
  6. return '$value'.padLeft(length, "0");
  7. }
  8. CustomPicker({DateTime currentTime, LocaleType locale})
  9. : super(locale: locale) {
  10. this.currentTime = currentTime ?? DateTime.now();
  11. this.setLeftIndex(this.currentTime.hour);
  12. this.setMiddleIndex(this.currentTime.minute);
  13. this.setRightIndex(this.currentTime.second);
  14. }
  15. @override
  16. String leftStringAtIndex(int index) {
  17. if (index >= 0 && index < 24) {
  18. return this.digits(index, 2);
  19. } else {
  20. return null;
  21. }
  22. }
  23. @override
  24. String middleStringAtIndex(int index) {
  25. if (index >= 0 && index < 60) {
  26. return this.digits(index, 2);
  27. } else {
  28. return null;
  29. }
  30. }
  31. @override
  32. String rightStringAtIndex(int index) {
  33. if (index >= 0 && index < 60) {
  34. return this.digits(index, 2);
  35. } else {
  36. return null;
  37. }
  38. }
  39. @override
  40. String leftDivider() {
  41. return "|";
  42. }
  43. @override
  44. String rightDivider() {
  45. return "|";
  46. }
  47. @override
  48. List<int> layoutProportions() {
  49. return [1, 2, 1];
  50. }
  51. @override
  52. DateTime finalTime() {
  53. return currentTime.isUtc
  54. ? DateTime.utc(
  55. currentTime.year,
  56. currentTime.month,
  57. currentTime.day,
  58. this.currentLeftIndex(),
  59. this.currentMiddleIndex(),
  60. this.currentRightIndex())
  61. : DateTime(
  62. currentTime.year,
  63. currentTime.month,
  64. currentTime.day,
  65. this.currentLeftIndex(),
  66. this.currentMiddleIndex(),
  67. this.currentRightIndex());
  68. }
  69. }
  70. class MyApp extends StatelessWidget {
  71. // This widget is the root of your application.
  72. @override
  73. Widget build(BuildContext context) {
  74. return new MaterialApp(
  75. title: 'Flutter Demo',
  76. theme: new ThemeData(
  77. primarySwatch: Colors.blue,
  78. ),
  79. home: new HomePage(),
  80. );
  81. }
  82. }
  83. class HomePage extends StatelessWidget {
  84. @override
  85. Widget build(BuildContext context) {
  86. return Scaffold(
  87. appBar: AppBar(
  88. title: Text('Datetime Picker'),
  89. ),
  90. body: Center(
  91. child: Column(
  92. children: <Widget>[
  93. FlatButton(
  94. onPressed: () {
  95. DatePicker.showDatePicker(context,
  96. showTitleActions: true,
  97. minTime: DateTime(2018, 3, 5),
  98. maxTime: DateTime(2019, 6, 7),
  99. theme: DatePickerTheme(
  100. backgroundColor: Colors.blue,
  101. itemStyle: TextStyle(
  102. color: Colors.white, fontWeight: FontWeight.bold),
  103. doneStyle:
  104. TextStyle(color: Colors.white, fontSize: 16)),
  105. onChanged: (date) {
  106. print('change $date in time zone ' +
  107. date.timeZoneOffset.inHours.toString());
  108. }, onConfirm: (date) {
  109. print('confirm $date');
  110. }, currentTime: DateTime.now(), locale: LocaleType.en);
  111. },
  112. child: Text(
  113. 'show date picker(custom theme &date time range)',
  114. style: TextStyle(color: Colors.blue),
  115. )),
  116. FlatButton(
  117. onPressed: () {
  118. DatePicker.showTimePicker(context, showTitleActions: true,
  119. onChanged: (date) {
  120. print('change $date in time zone ' +
  121. date.timeZoneOffset.inHours.toString());
  122. }, onConfirm: (date) {
  123. print('confirm $date');
  124. }, currentTime: DateTime.now());
  125. },
  126. child: Text(
  127. 'show time picker',
  128. style: TextStyle(color: Colors.blue),
  129. )),
  130. FlatButton(
  131. onPressed: () {
  132. DatePicker.showDateTimePicker(context,
  133. showTitleActions: true,
  134. minTime: DateTime(2020, 5, 5, 20, 50),
  135. maxTime: DateTime(2020, 6, 7, 05, 09), onChanged: (date) {
  136. print('change $date in time zone ' +
  137. date.timeZoneOffset.inHours.toString());
  138. }, onConfirm: (date) {
  139. print('confirm $date');
  140. }, locale: LocaleType.zh);
  141. },
  142. child: Text(
  143. 'show date time picker (Chinese)',
  144. style: TextStyle(color: Colors.blue),
  145. )),
  146. FlatButton(
  147. onPressed: () {
  148. DatePicker.showDateTimePicker(context, showTitleActions: true,
  149. onChanged: (date) {
  150. print('change $date in time zone ' +
  151. date.timeZoneOffset.inHours.toString());
  152. }, onConfirm: (date) {
  153. print('confirm $date');
  154. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34));
  155. },
  156. child: Text(
  157. 'show date time picker (English-America)',
  158. style: TextStyle(color: Colors.blue),
  159. )),
  160. FlatButton(
  161. onPressed: () {
  162. DatePicker.showDateTimePicker(context, showTitleActions: true,
  163. onChanged: (date) {
  164. print('change $date in time zone ' +
  165. date.timeZoneOffset.inHours.toString());
  166. }, onConfirm: (date) {
  167. print('confirm $date');
  168. },
  169. currentTime: DateTime(2008, 12, 31, 23, 12, 34),
  170. locale: LocaleType.nl);
  171. },
  172. child: Text(
  173. 'show date time picker (Dutch)',
  174. style: TextStyle(color: Colors.blue),
  175. )),
  176. FlatButton(
  177. onPressed: () {
  178. DatePicker.showDateTimePicker(context, showTitleActions: true,
  179. onChanged: (date) {
  180. print('change $date in time zone ' +
  181. date.timeZoneOffset.inHours.toString());
  182. }, onConfirm: (date) {
  183. print('confirm $date');
  184. },
  185. currentTime: DateTime(2008, 12, 31, 23, 12, 34),
  186. locale: LocaleType.ru);
  187. },
  188. child: Text(
  189. 'show date time picker (Russian)',
  190. style: TextStyle(color: Colors.blue),
  191. )),
  192. FlatButton(
  193. onPressed: () {
  194. DatePicker.showDateTimePicker(context, showTitleActions: true,
  195. onChanged: (date) {
  196. print('change $date in time zone ' +
  197. date.timeZoneOffset.inHours.toString());
  198. }, onConfirm: (date) {
  199. print('confirm $date');
  200. },
  201. currentTime: DateTime.utc(2019, 12, 31, 23, 12, 34),
  202. locale: LocaleType.de);
  203. },
  204. child: Text(
  205. 'show date time picker in UTC (German)',
  206. style: TextStyle(color: Colors.blue),
  207. )),
  208. FlatButton(
  209. onPressed: () {
  210. DatePicker.showPicker(context, showTitleActions: true,
  211. onChanged: (date) {
  212. print('change $date in time zone ' +
  213. date.timeZoneOffset.inHours.toString());
  214. }, onConfirm: (date) {
  215. print('confirm $date');
  216. },
  217. pickerModel: CustomPicker(currentTime: DateTime.now()),
  218. locale: LocaleType.en);
  219. },
  220. child: Text(
  221. 'show custom time picker,\nyou can custom picker model like this',
  222. style: TextStyle(color: Colors.blue),
  223. )),
  224. ],
  225. ),
  226. ),
  227. );
  228. }
  229. }