main.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. FlatButton(
  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. backgroundColor: Colors.blue,
  90. itemStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
  91. doneStyle: TextStyle(color: Colors.white, fontSize: 16)),
  92. onChanged: (date) {
  93. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  94. }, onConfirm: (date) {
  95. print('confirm $date');
  96. }, currentTime: DateTime.now(), locale: LocaleType.en);
  97. },
  98. child: Text(
  99. 'show date picker(custom theme &date time range)',
  100. style: TextStyle(color: Colors.blue),
  101. )),
  102. FlatButton(
  103. onPressed: () {
  104. DatePicker.showTimePicker(context, showTitleActions: true, onChanged: (date) {
  105. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  106. }, onConfirm: (date) {
  107. print('confirm $date');
  108. }, currentTime: DateTime.now());
  109. },
  110. child: Text(
  111. 'show time picker',
  112. style: TextStyle(color: Colors.blue),
  113. )),
  114. FlatButton(
  115. onPressed: () {
  116. DatePicker.showDateTimePicker(context,
  117. showTitleActions: true,
  118. minTime: DateTime(2018, 5, 5, 20, 50),
  119. maxTime: DateTime(2018, 6, 7, 05, 09), onChanged: (date) {
  120. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  121. }, onConfirm: (date) {
  122. print('confirm $date');
  123. }, currentTime: DateTime(2018, 5, 31, 23, 12, 34), locale: LocaleType.zh);
  124. },
  125. child: Text(
  126. 'show date time picker (Chinese)',
  127. style: TextStyle(color: Colors.blue),
  128. )),
  129. FlatButton(
  130. onPressed: () {
  131. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
  132. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  133. }, onConfirm: (date) {
  134. print('confirm $date');
  135. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34));
  136. },
  137. child: Text(
  138. 'show date time picker (English-America)',
  139. style: TextStyle(color: Colors.blue),
  140. )),
  141. FlatButton(
  142. onPressed: () {
  143. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
  144. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  145. }, onConfirm: (date) {
  146. print('confirm $date');
  147. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34), locale: LocaleType.nl);
  148. },
  149. child: Text(
  150. 'show date time picker (Dutch)',
  151. style: TextStyle(color: Colors.blue),
  152. )),
  153. FlatButton(
  154. onPressed: () {
  155. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
  156. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  157. }, onConfirm: (date) {
  158. print('confirm $date');
  159. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34), locale: LocaleType.ru);
  160. },
  161. child: Text(
  162. 'show date time picker (Russian)',
  163. style: TextStyle(color: Colors.blue),
  164. )),
  165. FlatButton(
  166. onPressed: () {
  167. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
  168. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  169. }, onConfirm: (date) {
  170. print('confirm $date');
  171. }, currentTime: DateTime.utc(2019, 12, 31, 23, 12, 34), locale: LocaleType.de);
  172. },
  173. child: Text(
  174. 'show date time picker in UTC (German)',
  175. style: TextStyle(color: Colors.blue),
  176. )),
  177. FlatButton(
  178. onPressed: () {
  179. DatePicker.showPicker(context, showTitleActions: true, onChanged: (date) {
  180. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  181. }, onConfirm: (date) {
  182. print('confirm $date');
  183. }, pickerModel: CustomPicker(currentTime: DateTime.now()), locale: LocaleType.en);
  184. },
  185. child: Text(
  186. 'show custom time picker,\nyou can custom picker model like this',
  187. style: TextStyle(color: Colors.blue),
  188. )),
  189. ],
  190. ),
  191. ),
  192. );
  193. }
  194. }