ソースを参照

add customize example

Liu Yanbo 6 年 前
コミット
4c099b37c6
5 ファイル変更173 行追加45 行削除
  1. 2 1
      CHANGELOG.md
  2. 75 1
      README.md
  3. 95 42
      example/lib/main.dart
  4. BIN
      main_page.png
  5. 1 1
      pubspec.yaml

+ 2 - 1
CHANGELOG.md

@@ -30,4 +30,5 @@
 ## [1.1.8] - update something
 ## [1.1.8] - update something
 ## [1.1.9] - add German
 ## [1.1.9] - add German
 ## [1.2.0] - add support for time zone
 ## [1.2.0] - add support for time zone
-## [1.2.1] - add more languages
+## [1.2.1] - add more languages
+## [1.2.2] - add customize example

+ 75 - 1
README.md

@@ -36,6 +36,11 @@ International:
 | -------------------------------- | -------------------------------- | ------------------------------ | -------------------------------- |
 | -------------------------------- | -------------------------------- | ------------------------------ | -------------------------------- |
 | ![](screen_datetime_chinese.png) | ![](screen_datetime_english.png) | ![](screen_datetime_dutch.png) | ![](screen_datetime_russian.png) |
 | ![](screen_datetime_chinese.png) | ![](screen_datetime_english.png) | ![](screen_datetime_dutch.png) | ![](screen_datetime_russian.png) |
 
 
+
+## Demo App
+
+![main page](main_page.png)
+
 ## Usage
 ## Usage
 
 
 ```
 ```
@@ -56,10 +61,79 @@ FlatButton(
     ));
     ));
 ```
 ```
 
 
-## Custom
+## Customize
 
 
 If you want to customize your own style of date time picker, there is a class called CommonPickerModel, every type of date time picker is extended from this class, you can refer to other picker model (eg. DatePickerModel), and write your custom one, then pass this model to showPicker method, so that your own date time picker will appear, it’s easy, and will perfectly meet your demand
 If you want to customize your own style of date time picker, there is a class called CommonPickerModel, every type of date time picker is extended from this class, you can refer to other picker model (eg. DatePickerModel), and write your custom one, then pass this model to showPicker method, so that your own date time picker will appear, it’s easy, and will perfectly meet your demand
 
 
+How to customize your own picker model:
+
+```
+class CustomPicker extends CommonPickerModel {
+  String digits(int value, int length) {
+    return '$value'.padLeft(length, "0");
+  }
+
+  CustomPicker({DateTime currentTime, LocaleType locale}) : super(locale: locale) {
+    this.currentTime = currentTime ?? DateTime.now();
+    this.setLeftIndex(this.currentTime.hour);
+    this.setMiddleIndex(this.currentTime.minute);
+    this.setRightIndex(this.currentTime.second);
+  }
+
+  @override
+  String leftStringAtIndex(int index) {
+    if (index >= 0 && index < 24) {
+      return this.digits(index, 2);
+    } else {
+      return null;
+    }
+  }
+
+  @override
+  String middleStringAtIndex(int index) {
+    if (index >= 0 && index < 60) {
+      return this.digits(index, 2);
+    } else {
+      return null;
+    }
+  }
+
+  @override
+  String rightStringAtIndex(int index) {
+    if (index >= 0 && index < 60) {
+      return this.digits(index, 2);
+    } else {
+      return null;
+    }
+  }
+
+  @override
+  String leftDivider() {
+    return "|";
+  }
+
+  @override
+  String rightDivider() {
+    return "|";
+  }
+
+  @override
+  List<int> layoutProportions() {
+    return [1, 2, 1];
+  }
+
+  @override
+  DateTime finalTime() {
+    return currentTime.isUtc
+        ? DateTime.utc(currentTime.year, currentTime.month, currentTime.day,
+            this.currentLeftIndex(), this.currentMiddleIndex(), this.currentRightIndex())
+        : DateTime(currentTime.year, currentTime.month, currentTime.day, this.currentLeftIndex(),
+            this.currentMiddleIndex(), this.currentRightIndex());
+  }
+}
+```
+
+
 ## Getting Started
 ## Getting Started
 
 
 For help getting started with Flutter, view our online [documentation](https://flutter.io/).
 For help getting started with Flutter, view our online [documentation](https://flutter.io/).

+ 95 - 42
example/lib/main.dart

@@ -3,6 +3,70 @@ import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
 
 
 void main() => runApp(new MyApp());
 void main() => runApp(new MyApp());
 
 
+class CustomPicker extends CommonPickerModel {
+  String digits(int value, int length) {
+    return '$value'.padLeft(length, "0");
+  }
+
+  CustomPicker({DateTime currentTime, LocaleType locale}) : super(locale: locale) {
+    this.currentTime = currentTime ?? DateTime.now();
+    this.setLeftIndex(this.currentTime.hour);
+    this.setMiddleIndex(this.currentTime.minute);
+    this.setRightIndex(this.currentTime.second);
+  }
+
+  @override
+  String leftStringAtIndex(int index) {
+    if (index >= 0 && index < 24) {
+      return this.digits(index, 2);
+    } else {
+      return null;
+    }
+  }
+
+  @override
+  String middleStringAtIndex(int index) {
+    if (index >= 0 && index < 60) {
+      return this.digits(index, 2);
+    } else {
+      return null;
+    }
+  }
+
+  @override
+  String rightStringAtIndex(int index) {
+    if (index >= 0 && index < 60) {
+      return this.digits(index, 2);
+    } else {
+      return null;
+    }
+  }
+
+  @override
+  String leftDivider() {
+    return "|";
+  }
+
+  @override
+  String rightDivider() {
+    return "|";
+  }
+
+  @override
+  List<int> layoutProportions() {
+    return [1, 2, 1];
+  }
+
+  @override
+  DateTime finalTime() {
+    return currentTime.isUtc
+        ? DateTime.utc(currentTime.year, currentTime.month, currentTime.day,
+            this.currentLeftIndex(), this.currentMiddleIndex(), this.currentRightIndex())
+        : DateTime(currentTime.year, currentTime.month, currentTime.day, this.currentLeftIndex(),
+            this.currentMiddleIndex(), this.currentRightIndex());
+  }
+}
+
 class MyApp extends StatelessWidget {
 class MyApp extends StatelessWidget {
   // This widget is the root of your application.
   // This widget is the root of your application.
   @override
   @override
@@ -35,13 +99,10 @@ class HomePage extends StatelessWidget {
                       maxTime: DateTime(2019, 6, 7),
                       maxTime: DateTime(2019, 6, 7),
                       theme: DatePickerTheme(
                       theme: DatePickerTheme(
                           backgroundColor: Colors.blue,
                           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) {
                       onChanged: (date) {
-                    print('change $date in time zone ' +
-                        date.timeZoneOffset.inHours.toString());
+                    print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                   }, onConfirm: (date) {
                     print('confirm $date');
                     print('confirm $date');
                   }, currentTime: DateTime.now(), locale: LocaleType.en);
                   }, currentTime: DateTime.now(), locale: LocaleType.en);
@@ -52,10 +113,8 @@ class HomePage extends StatelessWidget {
                 )),
                 )),
             FlatButton(
             FlatButton(
                 onPressed: () {
                 onPressed: () {
-                  DatePicker.showTimePicker(context, showTitleActions: true,
-                      onChanged: (date) {
-                    print('change $date in time zone ' +
-                        date.timeZoneOffset.inHours.toString());
+                  DatePicker.showTimePicker(context, showTitleActions: true, onChanged: (date) {
+                    print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                   }, onConfirm: (date) {
                     print('confirm $date');
                     print('confirm $date');
                   }, currentTime: DateTime.now());
                   }, currentTime: DateTime.now());
@@ -66,15 +125,11 @@ class HomePage extends StatelessWidget {
                 )),
                 )),
             FlatButton(
             FlatButton(
                 onPressed: () {
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true,
-                      onChanged: (date) {
-                    print('change $date in time zone ' +
-                        date.timeZoneOffset.inHours.toString());
+                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
+                    print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                   }, onConfirm: (date) {
                     print('confirm $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(
                 child: Text(
                   'show date time picker (Chinese)',
                   'show date time picker (Chinese)',
@@ -82,10 +137,8 @@ class HomePage extends StatelessWidget {
                 )),
                 )),
             FlatButton(
             FlatButton(
                 onPressed: () {
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true,
-                      onChanged: (date) {
-                    print('change $date in time zone ' +
-                        date.timeZoneOffset.inHours.toString());
+                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
+                    print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                   }, onConfirm: (date) {
                     print('confirm $date');
                     print('confirm $date');
                   }, currentTime: DateTime(2008, 12, 31, 23, 12, 34));
                   }, currentTime: DateTime(2008, 12, 31, 23, 12, 34));
@@ -96,15 +149,11 @@ class HomePage extends StatelessWidget {
                 )),
                 )),
             FlatButton(
             FlatButton(
                 onPressed: () {
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true,
-                      onChanged: (date) {
-                    print('change $date in time zone ' +
-                        date.timeZoneOffset.inHours.toString());
+                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
+                    print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                   }, onConfirm: (date) {
                     print('confirm $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(
                 child: Text(
                   'show date time picker (Dutch)',
                   'show date time picker (Dutch)',
@@ -112,15 +161,11 @@ class HomePage extends StatelessWidget {
                 )),
                 )),
             FlatButton(
             FlatButton(
                 onPressed: () {
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true,
-                      onChanged: (date) {
-                    print('change $date in time zone ' +
-                        date.timeZoneOffset.inHours.toString());
+                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
+                    print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                   }, onConfirm: (date) {
                     print('confirm $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(
                 child: Text(
                   'show date time picker (Russian)',
                   'show date time picker (Russian)',
@@ -128,20 +173,28 @@ class HomePage extends StatelessWidget {
                 )),
                 )),
             FlatButton(
             FlatButton(
                 onPressed: () {
                 onPressed: () {
-                  DatePicker.showDateTimePicker(context, showTitleActions: true,
-                      onChanged: (date) {
-                    print('change $date in time zone ' +
-                        date.timeZoneOffset.inHours.toString());
+                  DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {
+                    print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
                   }, onConfirm: (date) {
                   }, onConfirm: (date) {
                     print('confirm $date');
                     print('confirm $date');
-                  },
-                      currentTime: DateTime.utc(2019, 12, 31, 23, 12, 34),
-                      locale: LocaleType.de);
+                  }, currentTime: DateTime.utc(2019, 12, 31, 23, 12, 34), locale: LocaleType.de);
                 },
                 },
                 child: Text(
                 child: Text(
                   'show date time picker in UTC (German)',
                   'show date time picker in UTC (German)',
                   style: TextStyle(color: Colors.blue),
                   style: TextStyle(color: Colors.blue),
                 )),
                 )),
+            FlatButton(
+                onPressed: () {
+                  DatePicker.showPicker(context, showTitleActions: true, onChanged: (date) {
+                    print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
+                  }, onConfirm: (date) {
+                    print('confirm $date');
+                  }, pickerModel: CustomPicker(currentTime: DateTime.now()), locale: LocaleType.en);
+                },
+                child: Text(
+                  'show custom time picker,\nyou can custom picker model like this',
+                  style: TextStyle(color: Colors.blue),
+                )),
           ],
           ],
         ),
         ),
       ),
       ),

BIN
main_page.png


+ 1 - 1
pubspec.yaml

@@ -1,6 +1,6 @@
 name: flutter_datetime_picker
 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
 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.2.1
+version: 1.2.2
 author: Realank <realank@126.com>
 author: Realank <realank@126.com>
 homepage: https://github.com/Realank/flutter_datetime_picker
 homepage: https://github.com/Realank/flutter_datetime_picker