Pārlūkot izejas kodu

增加日期最大最小值的控制,
增加主题控制

chenrizhang 7 gadi atpakaļ
vecāks
revīzija
79de63a1fe
2 mainītis faili ar 35 papildinājumiem un 17 dzēšanām
  1. 24 3
      lib/datetime_picker_theme_data.dart
  2. 11 14
      lib/flutter_datetime_picker.dart

+ 24 - 3
lib/datetime_picker_theme_data.dart

@@ -16,18 +16,33 @@ class DatePickerThemeData extends Diagnosticable {
   final Color backgroundColor;
   final Color barrierColor;
 
-  const DatePickerThemeData({this.inherit, this.cancelStyle, this.doneStyle, this.itemStyle, this.backgroundColor, this.barrierColor});
+  final double containerHeight;
+  final double titleHeight;
+  final double itemHeight;
+
+  const DatePickerThemeData({
+    this.inherit,
+    this.cancelStyle,
+    this.doneStyle,
+    this.itemStyle,
+    this.backgroundColor,
+    this.barrierColor,
+    this.containerHeight = 210.0,
+    this.titleHeight = 44.0,
+    this.itemHeight = 36.0,
+  });
 
   /// Creates a copy of this theme data but with the given fields replaced with
   /// the new values.
   DatePickerThemeData copyWith({
-    String cancelText,
     TextStyle cancelStyle,
-    String doneText,
     TextStyle doneStyle,
     TextStyle itemStyle,
     Color backgroundColor,
     Color barrierColor,
+    double pickerHeight,
+    double pickerTitleHeight,
+    double pickerItemHeight,
   }) {
     return DatePickerThemeData(
       inherit: inherit,
@@ -36,6 +51,9 @@ class DatePickerThemeData extends Diagnosticable {
       itemStyle: itemStyle != null ? itemStyle.merge(this.itemStyle) : this.itemStyle,
       backgroundColor: backgroundColor ?? this.backgroundColor,
       barrierColor: barrierColor ?? this.barrierColor,
+      containerHeight: pickerHeight ?? this.containerHeight,
+      titleHeight: pickerTitleHeight ?? this.titleHeight,
+      itemHeight: pickerItemHeight ?? this.itemHeight,
     );
   }
 
@@ -63,6 +81,9 @@ class DatePickerThemeData extends Diagnosticable {
       itemStyle: other.itemStyle,
       backgroundColor: other.backgroundColor,
       barrierColor: other.barrierColor,
+      pickerHeight: other.containerHeight,
+      pickerTitleHeight: other.titleHeight,
+      pickerItemHeight: other.itemHeight,
     );
   }
 }

+ 11 - 14
lib/flutter_datetime_picker.dart

@@ -13,10 +13,6 @@ export 'package:flutter_datetime_picker/src/i18n_model.dart';
 typedef DateChangedCallback(DateTime time);
 typedef String StringAtIndexCallBack(int index);
 
-const double _kDatePickerHeight = 210.0;
-const double _kDatePickerTitleHeight = 44.0;
-const double _kDatePickerItemHeight = 36.0;
-
 class DatePicker {
   ///
   /// Display date picker bottom sheet.
@@ -225,7 +221,7 @@ class _DatePickerState extends State<_DatePickerComponent> {
         builder: (BuildContext context, Widget child) {
           return new ClipRect(
             child: new CustomSingleChildLayout(
-              delegate: new _BottomPickerLayout(widget.route.animation.value, showTitleActions: widget.route.showTitleActions),
+              delegate: new _BottomPickerLayout(widget.route.animation.value, theme, showTitleActions: widget.route.showTitleActions),
               child: new GestureDetector(
                 child: Material(
                   color: Colors.transparent,
@@ -264,7 +260,7 @@ class _DatePickerState extends State<_DatePickerComponent> {
       flex: layoutProportion,
       child: Container(
           padding: EdgeInsets.all(8.0),
-          height: _kDatePickerHeight,
+          height: theme.containerHeight,
           decoration: BoxDecoration(color: theme?.backgroundColor ?? Colors.white),
           child: NotificationListener(
               onNotification: (ScrollNotification notification) {
@@ -282,7 +278,7 @@ class _DatePickerState extends State<_DatePickerComponent> {
                   key: key,
                   backgroundColor: theme?.backgroundColor ?? Colors.white,
                   scrollController: scrollController,
-                  itemExtent: _kDatePickerItemHeight,
+                  itemExtent: theme.itemHeight,
                   onSelectedItemChanged: (int index) {
                     selectedChangedWhenScrolling(index);
                   },
@@ -293,7 +289,7 @@ class _DatePickerState extends State<_DatePickerComponent> {
                       return null;
                     }
                     return Container(
-                      height: _kDatePickerItemHeight,
+                      height: theme.itemHeight,
                       alignment: Alignment.center,
                       child: Text(
                         content,
@@ -353,13 +349,13 @@ class _DatePickerState extends State<_DatePickerComponent> {
     String cancel = _localeCancel();
 
     return Container(
-      height: _kDatePickerTitleHeight,
+      height: theme.titleHeight,
       decoration: BoxDecoration(color: theme?.backgroundColor ?? Colors.white),
       child: Row(
         mainAxisAlignment: MainAxisAlignment.spaceBetween,
         children: <Widget>[
           Container(
-            height: _kDatePickerTitleHeight,
+            height: theme.titleHeight,
             child: FlatButton(
               child: Text(
                 '$cancel',
@@ -369,7 +365,7 @@ class _DatePickerState extends State<_DatePickerComponent> {
             ),
           ),
           Container(
-            height: _kDatePickerTitleHeight,
+            height: theme.titleHeight,
             child: FlatButton(
               child: Text(
                 '$done',
@@ -398,17 +394,18 @@ class _DatePickerState extends State<_DatePickerComponent> {
 }
 
 class _BottomPickerLayout extends SingleChildLayoutDelegate {
-  _BottomPickerLayout(this.progress, {this.itemCount, this.showTitleActions});
+  _BottomPickerLayout(this.progress, this.theme, {this.itemCount, this.showTitleActions});
 
   final double progress;
   final int itemCount;
   final bool showTitleActions;
+  final DatePickerThemeData theme;
 
   @override
   BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
-    double maxHeight = _kDatePickerHeight;
+    double maxHeight = theme.containerHeight;
     if (showTitleActions) {
-      maxHeight += _kDatePickerTitleHeight;
+      maxHeight += theme.titleHeight;
     }
 
     return new BoxConstraints(minWidth: constraints.maxWidth, maxWidth: constraints.maxWidth, minHeight: 0.0, maxHeight: maxHeight);