main.dart 8.1 KB

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