浏览代码

fix for time zone

Liu Yanbo 6 年之前
父节点
当前提交
edc618735e
共有 4 个文件被更改,包括 101 次插入47 次删除
  1. 2 1
      CHANGELOG.md
  2. 44 21
      example/lib/main.dart
  3. 54 24
      lib/src/date_model.dart
  4. 1 1
      pubspec.yaml

+ 2 - 1
CHANGELOG.md

@@ -28,4 +28,5 @@
 ## [1.1.6] - update something
 ## [1.1.7] - update something
 ## [1.1.8] - update something
-## [1.1.9] - add German
+## [1.1.9] - add German
+## [1.2.09] - add support for time zone

+ 44 - 21
example/lib/main.dart

@@ -35,10 +35,13 @@ class HomePage extends StatelessWidget {
                       maxTime: DateTime(2019, 6, 7),
                       theme: DatePickerTheme(
                           backgroundColor: Colors.blue,
-                          itemStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
-                          doneStyle: TextStyle(color: Colors.white, fontSize: 16)),
+                          itemStyle: TextStyle(
+                              color: Colors.white, fontWeight: FontWeight.bold),
+                          doneStyle:
+                              TextStyle(color: Colors.white, fontSize: 16)),
                       onChanged: (date) {
-                    print('change $date');
+                    print('change $date in time zone ' +
+                        date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                     print('confirm $date');
                   }, currentTime: DateTime.now(), locale: LocaleType.en);
@@ -49,11 +52,13 @@ class HomePage extends StatelessWidget {
                 )),
             FlatButton(
                 onPressed: () {
-                  DatePicker.showTimePicker(context, showTitleActions: true, onChanged: (date) {
-                    print('change $date ' + date.timeZoneOffset.toString());
+                  DatePicker.showTimePicker(context, showTitleActions: true,
+                      onChanged: (date) {
+                    print('change $date in time zone ' +
+                        date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                     print('confirm $date');
-                  }, currentTime: DateTime.now().toUtc());
+                  }, currentTime: DateTime.now());
                 },
                 child: Text(
                   'show time picker',
@@ -61,11 +66,15 @@ class HomePage extends StatelessWidget {
                 )),
             FlatButton(
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
-                    print('change $date');
+                  DatePicker.showDateTimePicker(context, showTitleActions: true,
+                      onChanged: (date) {
+                    print('change $date in time zone ' +
+                        date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                     print('confirm $date');
-                  }, currentTime: DateTime(2008, 12, 31, 23, 12, 34), locale: LocaleType.zh);
+                  },
+                      currentTime: DateTime(2008, 12, 31, 23, 12, 34),
+                      locale: LocaleType.zh);
                 },
                 child: Text(
                   'show date time picker (Chinese)',
@@ -73,8 +82,10 @@ class HomePage extends StatelessWidget {
                 )),
             FlatButton(
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
-                    print('change $date');
+                  DatePicker.showDateTimePicker(context, showTitleActions: true,
+                      onChanged: (date) {
+                    print('change $date in time zone ' +
+                        date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                     print('confirm $date');
                   }, currentTime: DateTime(2008, 12, 31, 23, 12, 34));
@@ -85,11 +96,15 @@ class HomePage extends StatelessWidget {
                 )),
             FlatButton(
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
-                    print('change $date');
+                  DatePicker.showDateTimePicker(context, showTitleActions: true,
+                      onChanged: (date) {
+                    print('change $date in time zone ' +
+                        date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                     print('confirm $date');
-                  }, currentTime: DateTime(2008, 12, 31, 23, 12, 34), locale: LocaleType.nl);
+                  },
+                      currentTime: DateTime(2008, 12, 31, 23, 12, 34),
+                      locale: LocaleType.nl);
                 },
                 child: Text(
                   'show date time picker (Dutch)',
@@ -97,11 +112,15 @@ class HomePage extends StatelessWidget {
                 )),
             FlatButton(
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
-                    print('change $date');
+                  DatePicker.showDateTimePicker(context, showTitleActions: true,
+                      onChanged: (date) {
+                    print('change $date in time zone ' +
+                        date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                     print('confirm $date');
-                  }, currentTime: DateTime(2008, 12, 31, 23, 12, 34), locale: LocaleType.ru);
+                  },
+                      currentTime: DateTime(2008, 12, 31, 23, 12, 34),
+                      locale: LocaleType.ru);
                 },
                 child: Text(
                   'show date time picker (Russian)',
@@ -109,14 +128,18 @@ class HomePage extends StatelessWidget {
                 )),
             FlatButton(
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
-                    print('change $date');
+                  DatePicker.showDateTimePicker(context, showTitleActions: true,
+                      onChanged: (date) {
+                    print('change $date in time zone ' +
+                        date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                     print('confirm $date');
-                  }, currentTime: DateTime(2019, 12, 31, 23, 12, 34), locale: LocaleType.de);
+                  },
+                      currentTime: DateTime.utc(2019, 12, 31, 23, 12, 34),
+                      locale: LocaleType.de);
                 },
                 child: Text(
-                  'show date time picker (German)',
+                  'show date time picker in UTC (German)',
                   style: TextStyle(color: Colors.blue),
                 )),
           ],

+ 54 - 24
lib/src/date_model.dart

@@ -203,17 +203,29 @@ class DatePickerModel extends CommonPickerModel {
     DateTime newTime;
     //change date time
     if (currentTime.month == 2 && currentTime.day == 29) {
-      newTime = DateTime(
-        destYear,
-        currentTime.month,
-        calcDateCount(destYear, 2),
-      );
+      newTime = currentTime.isUtc
+          ? DateTime.utc(
+              destYear,
+              currentTime.month,
+              calcDateCount(destYear, 2),
+            )
+          : DateTime(
+              destYear,
+              currentTime.month,
+              calcDateCount(destYear, 2),
+            );
     } else {
-      newTime = DateTime(
-        destYear,
-        currentTime.month,
-        currentTime.day,
-      );
+      newTime = currentTime.isUtc
+          ? DateTime.utc(
+              destYear,
+              currentTime.month,
+              currentTime.day,
+            )
+          : DateTime(
+              destYear,
+              currentTime.month,
+              currentTime.day,
+            );
     }
     //min/max check
     if (newTime.isAfter(maxTime)) {
@@ -241,11 +253,17 @@ class DatePickerModel extends CommonPickerModel {
     DateTime newTime;
     //change date time
     int dayCount = calcDateCount(currentTime.year, destMonth);
-    newTime = DateTime(
-      currentTime.year,
-      destMonth,
-      currentTime.day <= dayCount ? currentTime.day : dayCount,
-    );
+    newTime = currentTime.isUtc
+        ? DateTime.utc(
+            currentTime.year,
+            destMonth,
+            currentTime.day <= dayCount ? currentTime.day : dayCount,
+          )
+        : DateTime(
+            currentTime.year,
+            destMonth,
+            currentTime.day <= dayCount ? currentTime.day : dayCount,
+          );
     //min/max check
     if (newTime.isAfter(maxTime)) {
       currentTime = maxTime;
@@ -264,11 +282,17 @@ class DatePickerModel extends CommonPickerModel {
   void setRightIndex(int index) {
     super.setRightIndex(index);
     int minDay = _minDayOfCurrentMonth();
-    currentTime = DateTime(
-      currentTime.year,
-      currentTime.month,
-      minDay + index,
-    );
+    currentTime = currentTime.isUtc
+        ? DateTime.utc(
+            currentTime.year,
+            currentTime.month,
+            minDay + index,
+          )
+        : DateTime(
+            currentTime.year,
+            currentTime.month,
+            minDay + index,
+          );
   }
 
   @override
@@ -385,8 +409,11 @@ class TimePickerModel extends CommonPickerModel {
 
   @override
   DateTime finalTime() {
-    return DateTime(currentTime.year, currentTime.month, currentTime.day,
-        _currentLeftIndex, _currentMiddleIndex, _currentRightIndex);
+    return currentTime.isUtc
+        ? DateTime.utc(currentTime.year, currentTime.month, currentTime.day,
+            _currentLeftIndex, _currentMiddleIndex, _currentRightIndex)
+        : DateTime(currentTime.year, currentTime.month, currentTime.day,
+            _currentLeftIndex, _currentMiddleIndex, _currentRightIndex);
   }
 }
 
@@ -427,8 +454,11 @@ class DateTimePickerModel extends CommonPickerModel {
   @override
   DateTime finalTime() {
     DateTime time = currentTime.add(Duration(days: _currentLeftIndex));
-    return DateTime(time.year, time.month, time.day, _currentMiddleIndex,
-        _currentRightIndex);
+    return currentTime.isUtc
+        ? DateTime.utc(time.year, time.month, time.day, _currentMiddleIndex,
+            _currentRightIndex)
+        : DateTime(time.year, time.month, time.day, _currentMiddleIndex,
+            _currentRightIndex);
   }
 
   @override

+ 1 - 1
pubspec.yaml

@@ -1,6 +1,6 @@
 name: flutter_datetime_picker
 description: A date time picker for flutter, you can choose date / time / date&time in English Dutch and Chinese, and you can also custom your own picker content
-version: 1.1.9
+version: 1.2.0
 author: Realank <realank@126.com>
 homepage: https://github.com/Realank/flutter_datetime_picker