diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e99681..3007741 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.0.0 + +- Controller has to be specified given to the [DragDownDateTimePicker] so the date can be changed outside of the widget itself. + ## 3.3.3 - Added option for date box theme for marked dates and a boolean to use it diff --git a/example/lib/main.dart b/example/lib/main.dart index 87251be..2808be4 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -156,32 +156,36 @@ class DatePickerDemo extends StatelessWidget { ), DragDownDateTimePicker( onTimerPickerSheetChange: (value) {}, - alwaysUse24HourFormat: true, - dateTimePickerTheme: const DateTimePickerTheme( - backgroundColor: Colors.white, - markedIndicatorColor: Colors.red, - baseTheme: DateBoxTheme( + controller: DateTimePickerController( + initialDate: DateTime.now(), + highlightToday: true, + alwaysUse24HourFormat: true, + markedDates: [DateTime.now().subtract(const Duration(days: 1))], + theme: const DateTimePickerTheme( backgroundColor: Colors.white, - textStyle: TextStyle(color: Colors.black), - ), - selectedTheme: DateBoxTheme( - backgroundColor: Color(0x4BF44336), - textStyle: TextStyle( - color: Colors.red, + markedIndicatorColor: Colors.red, + baseTheme: DateBoxTheme( + backgroundColor: Colors.white, + textStyle: TextStyle(color: Colors.black), ), - ), - highlightTheme: DateBoxTheme( - backgroundColor: Colors.red, - textStyle: TextStyle( - color: Colors.white, + selectedTheme: DateBoxTheme( + backgroundColor: Color(0x4BF44336), + textStyle: TextStyle( + color: Colors.red, + ), + ), + highlightTheme: DateBoxTheme( + backgroundColor: Colors.red, + textStyle: TextStyle( + color: Colors.white, + ), + ), + barTheme: DateTimePickerBarTheme( + barColor: Colors.black, + barOpacity: 1, ), - ), - barTheme: DateTimePickerBarTheme( - barColor: Colors.black, - barOpacity: 1, ), ), - markedDates: [DateTime.now().subtract(const Duration(days: 1))], ), ], ), diff --git a/example/pubspec.lock b/example/pubspec.lock index d09e770..633a761 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -68,7 +68,7 @@ packages: path: ".." relative: true source: path - version: "3.3.2" + version: "3.3.3" flutter_lints: dependency: "direct dev" description: diff --git a/lib/flutter_date_time_picker.dart b/lib/flutter_date_time_picker.dart index 576c90f..4b439f5 100644 --- a/lib/flutter_date_time_picker.dart +++ b/lib/flutter_date_time_picker.dart @@ -5,6 +5,7 @@ library flutter_date_time_picker; export 'src/drag_down_date_time_picker.dart' show DragDownDateTimePicker; +export 'src/utils/date_time_picker_controller.dart'; export 'src/overlay_date_time_picker.dart' show OverlayDateTimePicker; export 'src/models/date_constraint.dart'; export 'src/enums/date_box_shape.dart'; diff --git a/lib/src/drag_down_date_time_picker.dart b/lib/src/drag_down_date_time_picker.dart index 4b74782..1d5caae 100644 --- a/lib/src/drag_down_date_time_picker.dart +++ b/lib/src/drag_down_date_time_picker.dart @@ -3,7 +3,7 @@ // SPDX-License-Identifier: BSD-3-Clause import 'package:flutter/material.dart'; -import 'package:flutter_date_time_picker/src/models/date_time_picker_theme.dart'; + import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; import 'package:flutter_date_time_picker/src/widgets/month_date_time_picker/month_date_time_picker_sheet.dart'; import 'package:flutter_date_time_picker/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart'; @@ -15,59 +15,21 @@ class DragDownDateTimePicker extends StatefulWidget { /// Both views can be dragged sideways to show the next or previous week/month. const DragDownDateTimePicker({ - this.dateTimePickerTheme = const DateTimePickerTheme(), - this.header, + required this.controller, this.onTimerPickerSheetChange, - this.onTapDay, - this.highlightToday = true, this.wrongTimeDialog, - this.alwaysUse24HourFormat, - this.pickTime = false, - this.initialDate, - this.markedDates, - this.disabledDates, - this.disabledTimes, this.child, super.key, }); + final DateTimePickerController controller; + /// The child contained by the DatePicker. final Widget? child; /// A [Widget] to display when the user picks a disabled time in the [TimePickerDialog] final Widget? wrongTimeDialog; - /// Visual properties for the [DragDownDateTimePicker] - final DateTimePickerTheme dateTimePickerTheme; - - /// Widget shown at the top of the [DragDownDateTimePicker] - final Widget? header; - - /// Callback that provides the date tapped on as a [DateTime] object. - final Function(DateTime)? onTapDay; - - /// Whether the current day should be highlighted in the [DragDownDateTimePicker] - final bool highlightToday; - - /// a [bool] to set de clock on [TimePickerDialog] to a fixed 24 or 12-hour format. - /// By default this gets determined by the settings on the user device. - final bool? alwaysUse24HourFormat; - - /// [pickTime] is a [bool] that determines if the user is able to pick a time after picking a date using the [TimePickerDialog]. - final bool pickTime; - - /// indicates the starting date. Default is [DateTime.now()] - final DateTime? initialDate; - - /// [markedDates] contain the dates [DateTime] that will be marked in the [DragDownDateTimePicker] by a small dot. - final List? markedDates; - - /// a [List] of [DateTime] objects that will be disabled and cannot be interacted with whatsoever. - final List? disabledDates; - - /// a [List] of [TimeOfDay] objects that cannot be picked in the [TimePickerDialog]. - final List? disabledTimes; - /// Function that gets called when the view changes from week to month or vice versa. /// The value is the amount of scrolledpixels. final Function(double)? onTimerPickerSheetChange; @@ -77,7 +39,8 @@ class DragDownDateTimePicker extends StatefulWidget { } class _DragDownDateTimePickerState extends State { - late DateTimePickerController _dateTimePickerController; + late final DateTimePickerController _dateTimePickerController = + widget.controller; final DraggableScrollableController _dragController = DraggableScrollableController(); @@ -87,20 +50,6 @@ class _DragDownDateTimePickerState extends State { super.initState(); initializeDateFormatting(); - _dateTimePickerController = DateTimePickerController( - highlightToday: widget.highlightToday, - alwaysUse24HourFormat: widget.alwaysUse24HourFormat, - pickTime: widget.pickTime, - theme: widget.dateTimePickerTheme, - header: widget.header, - markedDates: widget.markedDates, - disabledDates: widget.disabledDates, - disabledTimes: widget.disabledTimes, - onTapDayCallBack: widget.onTapDay, - browsingDate: widget.initialDate ?? DateTime.now(), - selectedDate: widget.initialDate ?? DateTime.now(), - ); - _dateTimePickerController.addListener(() { setState(() {}); }); @@ -166,16 +115,16 @@ class _DragDownDateTimePickerState extends State { ? WeekDateTimePickerSheet( dateTimePickerController: _dateTimePickerController, - weekDateBoxSize: widget - .dateTimePickerTheme.weekDateBoxSize, + weekDateBoxSize: + widget.controller.theme.weekDateBoxSize, ) : MonthDateTimePickerSheet( dateTimePickerController: _dateTimePickerController, - monthDateBoxSize: widget - .dateTimePickerTheme.monthDateBoxSize, - monthDatePadding: widget - .dateTimePickerTheme.monthDatePadding, + monthDateBoxSize: + widget.controller.theme.monthDateBoxSize, + monthDatePadding: + widget.controller.theme.monthDatePadding, ), ), ), diff --git a/lib/src/overlay_date_time_picker.dart b/lib/src/overlay_date_time_picker.dart index a8c5f39..c644b96 100644 --- a/lib/src/overlay_date_time_picker.dart +++ b/lib/src/overlay_date_time_picker.dart @@ -127,8 +127,7 @@ class _OverlayDateTimePickerState extends State { Navigator.of(context).pop(); } }, - browsingDate: widget.initialDate ?? DateTime.now(), - selectedDate: widget.initialDate ?? DateTime.now(), + initialDate: widget.initialDate ?? DateTime.now(), ); } diff --git a/lib/src/utils/date_time_picker_controller.dart b/lib/src/utils/date_time_picker_controller.dart index 7b72994..d8e18de 100644 --- a/lib/src/utils/date_time_picker_controller.dart +++ b/lib/src/utils/date_time_picker_controller.dart @@ -9,9 +9,8 @@ class DateTimePickerController extends ChangeNotifier { DateTimePickerController({ required this.theme, required this.highlightToday, - required this.pickTime, - required this.browsingDate, - required this.selectedDate, + required this.initialDate, + this.pickTime = false, this.alwaysUse24HourFormat, this.header, this.wrongTimeDialog, @@ -19,28 +18,48 @@ class DateTimePickerController extends ChangeNotifier { this.disabledDates, this.disabledTimes, this.onTapDayCallBack, + this.onBorderScrollCallback, }); - final PageController _pageController = PageController(initialPage: 1); - + /// Whether the current day should be highlighted in the [DragDownDateTimePicker] final bool highlightToday; + + /// a [bool] to set de clock on [TimePickerDialog] to a fixed 24 or 12-hour format. + /// By default this gets determined by the settings on the user device. final bool? alwaysUse24HourFormat; + /// Widget shown at the top of the [DragDownDateTimePicker] final Widget? header; final Widget? wrongTimeDialog; + /// Visual properties for the [DragDownDateTimePicker] final DateTimePickerTheme theme; + /// [markedDates] contain the dates [DateTime] that will be marked in the [DragDownDateTimePicker] by a small dot. final List? markedDates; + + /// a [List] of [DateTime] objects that will be disabled and cannot be interacted with whatsoever. final List? disabledDates; + + /// a [List] of [TimeOfDay] objects that cannot be picked in the [TimePickerDialog]. final List? disabledTimes; + + /// [pickTime] is a [bool] that determines if the user is able to pick a time after picking a date using the [TimePickerDialog]. final bool pickTime; + /// Callback that provides the date tapped on as a [DateTime] object. final Function(DateTime)? onTapDayCallBack; - DateTime browsingDate; - DateTime selectedDate; + /// Callback that provides the new date which is scroll to. If this is null the scroll feature is disabled. + final Function(DateTime)? onBorderScrollCallback; + + final DateTime initialDate; + + final PageController _pageController = PageController(initialPage: 1); + + late DateTime browsingDate = initialDate; + late DateTime selectedDate = initialDate; @override void dispose() { @@ -74,6 +93,17 @@ class DateTimePickerController extends ChangeNotifier { } } + void onBorderScroll(DateTime date) { + browsingDate = date; + selectedDate = date; + + notifyListeners(); + + onBorderScrollCallback?.call( + date, + ); + } + PageController get pageController => _pageController; void setBrowsingDate(DateTime date) { diff --git a/lib/src/widgets/date_time_picker/date_time_picker.dart b/lib/src/widgets/date_time_picker/date_time_picker.dart index 656df6c..5342ae7 100644 --- a/lib/src/widgets/date_time_picker/date_time_picker.dart +++ b/lib/src/widgets/date_time_picker/date_time_picker.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; import 'package:flutter_date_time_picker/flutter_date_time_picker.dart'; -import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; import 'package:flutter_date_time_picker/src/widgets/overlay_date_time_picker/overlay.dart'; class DateTimePicker extends StatefulWidget { @@ -105,8 +104,7 @@ class _DateTimePickerState extends State { Navigator.of(context).pop(); } }, - browsingDate: widget.initialDate ?? DateTime.now(), - selectedDate: widget.initialDate ?? DateTime.now(), + initialDate: widget.initialDate ?? DateTime.now(), ); @override Widget build(BuildContext context) { diff --git a/lib/src/widgets/overlay_date_time_picker/date_picker.dart b/lib/src/widgets/overlay_date_time_picker/date_picker.dart index d020819..0303feb 100644 --- a/lib/src/widgets/overlay_date_time_picker/date_picker.dart +++ b/lib/src/widgets/overlay_date_time_picker/date_picker.dart @@ -5,7 +5,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_date_time_picker/flutter_date_time_picker.dart'; import 'package:flutter_date_time_picker/src/extensions/date_time.dart'; -import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; import 'package:flutter_date_time_picker/src/widgets/overlay_date_time_picker/pickable_date.dart'; import 'package:intl/intl.dart'; diff --git a/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart b/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart index c3a253b..dc421eb 100644 --- a/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart +++ b/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart @@ -7,7 +7,6 @@ import 'package:flutter_date_time_picker/flutter_date_time_picker.dart'; import 'package:flutter_date_time_picker/src/extensions/date_time.dart'; import 'package:flutter_date_time_picker/src/extensions/time_of_day.dart'; import 'package:flutter_date_time_picker/src/models/date_box_current_theme.dart'; -import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; import 'package:flutter_date_time_picker/src/widgets/marked_icon.dart'; import 'package:intl/intl.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 7325b57..addf7c5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_date_time_picker description: A Flutter package for date and time picker. -version: 3.3.3 +version: 4.0.0 environment: sdk: ">=3.0.0 <4.0.0" diff --git a/test/flutter_date_time_picker_test.dart b/test/flutter_date_time_picker_test.dart index 8b9ef20..367dacf 100644 --- a/test/flutter_date_time_picker_test.dart +++ b/test/flutter_date_time_picker_test.dart @@ -13,7 +13,12 @@ void main() { home: Scaffold( appBar: AppBar(), body: DragDownDateTimePicker( - pickTime: false, + controller: DateTimePickerController( + theme: const DateTimePickerTheme(), + highlightToday: true, + initialDate: DateTime.now(), + pickTime: false, + ), child: Container(), ), ), @@ -27,7 +32,12 @@ void main() { home: Scaffold( appBar: AppBar(), body: DragDownDateTimePicker( - pickTime: false, + controller: DateTimePickerController( + theme: const DateTimePickerTheme(), + highlightToday: true, + initialDate: DateTime.now(), + pickTime: false, + ), child: Container(), ), ),