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