main.dart 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. TextButton(
  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. headerColor: Colors.orange,
  101. backgroundColor: Colors.blue,
  102. itemStyle: TextStyle(
  103. color: Colors.white,
  104. fontWeight: FontWeight.bold,
  105. fontSize: 18),
  106. doneStyle:
  107. TextStyle(color: Colors.white, fontSize: 16)),
  108. onChanged: (date) {
  109. print('change $date in time zone ' +
  110. date.timeZoneOffset.inHours.toString());
  111. }, onConfirm: (date) {
  112. print('confirm $date');
  113. }, currentTime: DateTime.now(), locale: LocaleType.en);
  114. },
  115. child: Text(
  116. 'show date picker(custom theme &date time range)',
  117. style: TextStyle(color: Colors.blue),
  118. )),
  119. TextButton(
  120. onPressed: () {
  121. DatePicker.showTimePicker(context, showTitleActions: true,
  122. onChanged: (date) {
  123. print('change $date in time zone ' +
  124. date.timeZoneOffset.inHours.toString());
  125. }, onConfirm: (date) {
  126. print('confirm $date');
  127. }, currentTime: DateTime.now());
  128. },
  129. child: Text(
  130. 'show time picker',
  131. style: TextStyle(color: Colors.blue),
  132. )),
  133. TextButton(
  134. onPressed: () {
  135. DatePicker.showTime12hPicker(context, showTitleActions: true,
  136. onChanged: (date) {
  137. print('change $date in time zone ' +
  138. date.timeZoneOffset.inHours.toString());
  139. }, onConfirm: (date) {
  140. print('confirm $date');
  141. }, currentTime: DateTime.now());
  142. },
  143. child: Text(
  144. 'show 12H time picker with AM/PM',
  145. style: TextStyle(color: Colors.blue),
  146. )),
  147. TextButton(
  148. onPressed: () {
  149. DatePicker.showDateTimePicker(context,
  150. showTitleActions: true,
  151. minTime: DateTime(2020, 5, 5, 20, 50),
  152. maxTime: DateTime(2020, 6, 7, 05, 09), onChanged: (date) {
  153. print('change $date in time zone ' +
  154. date.timeZoneOffset.inHours.toString());
  155. }, onConfirm: (date) {
  156. print('confirm $date');
  157. }, locale: LocaleType.zh);
  158. },
  159. child: Text(
  160. 'show date time picker (Chinese)',
  161. style: TextStyle(color: Colors.blue),
  162. )),
  163. TextButton(
  164. onPressed: () {
  165. DatePicker.showDateTimePicker(context, showTitleActions: true,
  166. onChanged: (date) {
  167. print('change $date in time zone ' +
  168. date.timeZoneOffset.inHours.toString());
  169. }, onConfirm: (date) {
  170. print('confirm $date');
  171. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34));
  172. },
  173. child: Text(
  174. 'show date time picker (English-America)',
  175. style: TextStyle(color: Colors.blue),
  176. )),
  177. TextButton(
  178. onPressed: () {
  179. DatePicker.showDateTimePicker(context, showTitleActions: true,
  180. onChanged: (date) {
  181. print('change $date in time zone ' +
  182. date.timeZoneOffset.inHours.toString());
  183. }, onConfirm: (date) {
  184. print('confirm $date');
  185. },
  186. currentTime: DateTime(2008, 12, 31, 23, 12, 34),
  187. locale: LocaleType.nl);
  188. },
  189. child: Text(
  190. 'show date time picker (Dutch)',
  191. style: TextStyle(color: Colors.blue),
  192. )),
  193. TextButton(
  194. onPressed: () {
  195. DatePicker.showDateTimePicker(context, showTitleActions: true,
  196. onChanged: (date) {
  197. print('change $date in time zone ' +
  198. date.timeZoneOffset.inHours.toString());
  199. }, onConfirm: (date) {
  200. print('confirm $date');
  201. },
  202. currentTime: DateTime(2008, 12, 31, 23, 12, 34),
  203. locale: LocaleType.ru);
  204. },
  205. child: Text(
  206. 'show date time picker (Russian)',
  207. style: TextStyle(color: Colors.blue),
  208. )),
  209. TextButton(
  210. onPressed: () {
  211. DatePicker.showDateTimePicker(context, showTitleActions: true,
  212. onChanged: (date) {
  213. print('change $date in time zone ' +
  214. date.timeZoneOffset.inHours.toString());
  215. }, onConfirm: (date) {
  216. print('confirm $date');
  217. },
  218. currentTime: DateTime.utc(2019, 12, 31, 23, 12, 34),
  219. locale: LocaleType.de);
  220. },
  221. child: Text(
  222. 'show date time picker in UTC (German)',
  223. style: TextStyle(color: Colors.blue),
  224. )),
  225. TextButton(
  226. onPressed: () {
  227. DatePicker.showPicker(context, showTitleActions: true,
  228. onChanged: (date) {
  229. print('change $date in time zone ' +
  230. date.timeZoneOffset.inHours.toString());
  231. }, onConfirm: (date) {
  232. print('confirm $date');
  233. },
  234. pickerModel: CustomPicker(currentTime: DateTime.now()),
  235. locale: LocaleType.en);
  236. },
  237. child: Text(
  238. 'show custom time picker,\nyou can custom picker model like this',
  239. style: TextStyle(color: Colors.blue),
  240. )),
  241. ],
  242. ),
  243. ),
  244. );
  245. }
  246. }