main.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. 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. FlatButton(
  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. FlatButton(
  134. onPressed: () {
  135. DatePicker.showDateTimePicker(context,
  136. showTitleActions: true,
  137. minTime: DateTime(2020, 5, 5, 20, 50),
  138. maxTime: DateTime(2020, 6, 7, 05, 09), onChanged: (date) {
  139. print('change $date in time zone ' +
  140. date.timeZoneOffset.inHours.toString());
  141. }, onConfirm: (date) {
  142. print('confirm $date');
  143. }, locale: LocaleType.zh);
  144. },
  145. child: Text(
  146. 'show date time picker (Chinese)',
  147. style: TextStyle(color: Colors.blue),
  148. )),
  149. FlatButton(
  150. onPressed: () {
  151. DatePicker.showDateTimePicker(context, showTitleActions: true,
  152. onChanged: (date) {
  153. print('change $date in time zone ' +
  154. date.timeZoneOffset.inHours.toString());
  155. }, onConfirm: (date) {
  156. print('confirm $date');
  157. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34));
  158. },
  159. child: Text(
  160. 'show date time picker (English-America)',
  161. style: TextStyle(color: Colors.blue),
  162. )),
  163. FlatButton(
  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. },
  172. currentTime: DateTime(2008, 12, 31, 23, 12, 34),
  173. locale: LocaleType.nl);
  174. },
  175. child: Text(
  176. 'show date time picker (Dutch)',
  177. style: TextStyle(color: Colors.blue),
  178. )),
  179. FlatButton(
  180. onPressed: () {
  181. DatePicker.showDateTimePicker(context, showTitleActions: true,
  182. onChanged: (date) {
  183. print('change $date in time zone ' +
  184. date.timeZoneOffset.inHours.toString());
  185. }, onConfirm: (date) {
  186. print('confirm $date');
  187. },
  188. currentTime: DateTime(2008, 12, 31, 23, 12, 34),
  189. locale: LocaleType.ru);
  190. },
  191. child: Text(
  192. 'show date time picker (Russian)',
  193. style: TextStyle(color: Colors.blue),
  194. )),
  195. FlatButton(
  196. onPressed: () {
  197. DatePicker.showDateTimePicker(context, showTitleActions: true,
  198. onChanged: (date) {
  199. print('change $date in time zone ' +
  200. date.timeZoneOffset.inHours.toString());
  201. }, onConfirm: (date) {
  202. print('confirm $date');
  203. },
  204. currentTime: DateTime.utc(2019, 12, 31, 23, 12, 34),
  205. locale: LocaleType.de);
  206. },
  207. child: Text(
  208. 'show date time picker in UTC (German)',
  209. style: TextStyle(color: Colors.blue),
  210. )),
  211. FlatButton(
  212. onPressed: () {
  213. DatePicker.showPicker(context, showTitleActions: true,
  214. onChanged: (date) {
  215. print('change $date in time zone ' +
  216. date.timeZoneOffset.inHours.toString());
  217. }, onConfirm: (date) {
  218. print('confirm $date');
  219. },
  220. pickerModel: CustomPicker(currentTime: DateTime.now()),
  221. locale: LocaleType.en);
  222. },
  223. child: Text(
  224. 'show custom time picker,\nyou can custom picker model like this',
  225. style: TextStyle(color: Colors.blue),
  226. )),
  227. ],
  228. ),
  229. ),
  230. );
  231. }
  232. }