main.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, showTitleActions: true, onChanged: (date) {
  117. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  118. }, onConfirm: (date) {
  119. print('confirm $date');
  120. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34), locale: LocaleType.zh);
  121. },
  122. child: Text(
  123. 'show date time picker (Chinese)',
  124. style: TextStyle(color: Colors.blue),
  125. )),
  126. FlatButton(
  127. onPressed: () {
  128. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
  129. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  130. }, onConfirm: (date) {
  131. print('confirm $date');
  132. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34));
  133. },
  134. child: Text(
  135. 'show date time picker (English-America)',
  136. style: TextStyle(color: Colors.blue),
  137. )),
  138. FlatButton(
  139. onPressed: () {
  140. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
  141. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  142. }, onConfirm: (date) {
  143. print('confirm $date');
  144. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34), locale: LocaleType.nl);
  145. },
  146. child: Text(
  147. 'show date time picker (Dutch)',
  148. style: TextStyle(color: Colors.blue),
  149. )),
  150. FlatButton(
  151. onPressed: () {
  152. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
  153. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  154. }, onConfirm: (date) {
  155. print('confirm $date');
  156. }, currentTime: DateTime(2008, 12, 31, 23, 12, 34), locale: LocaleType.ru);
  157. },
  158. child: Text(
  159. 'show date time picker (Russian)',
  160. style: TextStyle(color: Colors.blue),
  161. )),
  162. FlatButton(
  163. onPressed: () {
  164. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
  165. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  166. }, onConfirm: (date) {
  167. print('confirm $date');
  168. }, currentTime: DateTime.utc(2019, 12, 31, 23, 12, 34), locale: LocaleType.de);
  169. },
  170. child: Text(
  171. 'show date time picker in UTC (German)',
  172. style: TextStyle(color: Colors.blue),
  173. )),
  174. FlatButton(
  175. onPressed: () {
  176. DatePicker.showPicker(context, showTitleActions: true, onChanged: (date) {
  177. print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
  178. }, onConfirm: (date) {
  179. print('confirm $date');
  180. }, pickerModel: CustomPicker(currentTime: DateTime.now()), locale: LocaleType.en);
  181. },
  182. child: Text(
  183. 'show custom time picker,\nyou can custom picker model like this',
  184. style: TextStyle(color: Colors.blue),
  185. )),
  186. ],
  187. ),
  188. ),
  189. );
  190. }
  191. }