diff --git a/.gitignore b/.gitignore index bb431f0..e89bb2a 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,8 @@ .pub-cache/ .pub/ build/ +pubspec.lock +**/pubspec.lock # Android related **/android/**/gradle-wrapper.jar diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d35f04..65f54df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +##3.0.0 + +* Breaking Upgraded minimum Dart version to 2.15 for Constructor Tearoff support +* Added optional `DateTimeFieldCreator` argument to `DateTimeFormField` to allow specifying custom implementations of `DateTimeField` +* Refactored `DateTimeField` to expose `showMaterialTimePicker`, `showMaterialDatePicker`, and `showMaterialTimePicker` as separate overrideable methods +* Made `kCupertinoDatePickerHeight` and `cupertinoModeFromPickerMode` public for convenience + ##2.1.3 * [TheGlorySaint](https://github.com/TheGlorySaint) added the possibility to use the 24Hour Format at the Timepicker. Default it is set to false diff --git a/README.md b/README.md index bd4ed6f..f5829db 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency: ```yaml dependencies: ... - date_field: ^2.1.2 + date_field: ^3.0.0 ``` In your library add the following import: diff --git a/example/pubspec.lock b/example/pubspec.lock deleted file mode 100644 index 84d66c5..0000000 --- a/example/pubspec.lock +++ /dev/null @@ -1,167 +0,0 @@ -# Generated by pub -# See https://dart.dev/tools/pub/glossary#lockfile -packages: - async: - dependency: transitive - description: - name: async - url: "https://pub.dartlang.org" - source: hosted - version: "2.5.0" - boolean_selector: - dependency: transitive - description: - name: boolean_selector - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.0" - characters: - dependency: transitive - description: - name: characters - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.0" - charcode: - dependency: transitive - description: - name: charcode - url: "https://pub.dartlang.org" - source: hosted - version: "1.2.0" - clock: - dependency: transitive - description: - name: clock - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.0" - collection: - dependency: transitive - description: - name: collection - url: "https://pub.dartlang.org" - source: hosted - version: "1.15.0" - cupertino_icons: - dependency: "direct main" - description: - name: cupertino_icons - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.2" - date_field: - dependency: "direct dev" - description: - path: ".." - relative: true - source: path - version: "1.0.5" - fake_async: - dependency: transitive - description: - name: fake_async - url: "https://pub.dartlang.org" - source: hosted - version: "1.2.0" - flutter: - dependency: "direct main" - description: flutter - source: sdk - version: "0.0.0" - flutter_test: - dependency: "direct dev" - description: flutter - source: sdk - version: "0.0.0" - intl: - dependency: transitive - description: - name: intl - url: "https://pub.dartlang.org" - source: hosted - version: "0.17.0" - matcher: - dependency: transitive - description: - name: matcher - url: "https://pub.dartlang.org" - source: hosted - version: "0.12.10" - meta: - dependency: transitive - description: - name: meta - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" - path: - dependency: transitive - description: - name: path - url: "https://pub.dartlang.org" - source: hosted - version: "1.8.0" - sky_engine: - dependency: transitive - description: flutter - source: sdk - version: "0.0.99" - source_span: - dependency: transitive - description: - name: source_span - url: "https://pub.dartlang.org" - source: hosted - version: "1.8.0" - stack_trace: - dependency: transitive - description: - name: stack_trace - url: "https://pub.dartlang.org" - source: hosted - version: "1.10.0" - stream_channel: - dependency: transitive - description: - name: stream_channel - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.0" - string_scanner: - dependency: transitive - description: - name: string_scanner - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.0" - term_glyph: - dependency: transitive - description: - name: term_glyph - url: "https://pub.dartlang.org" - source: hosted - version: "1.2.0" - test_api: - dependency: transitive - description: - name: test_api - url: "https://pub.dartlang.org" - source: hosted - version: "0.2.19" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" - vector_math: - dependency: transitive - description: - name: vector_math - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.0" -sdks: - dart: ">=2.12.0 <3.0.0" diff --git a/lib/src/field.dart b/lib/src/field.dart index 681630c..13c4c46 100644 --- a/lib/src/field.dart +++ b/lib/src/field.dart @@ -5,13 +5,33 @@ import 'package:intl/intl.dart'; final DateTime _kDefaultFirstSelectableDate = DateTime(1900); final DateTime _kDefaultLastSelectableDate = DateTime(2100); -const double _kCupertinoDatePickerHeight = 216; +const double kCupertinoDatePickerHeight = 216; + +/// Constructor tearoff definition that matches [DateTimeField.new] +// Note: This should match the definition of the [DateTimeField] constructor +typedef DateTimeFieldCreator = DateTimeField Function({ + Key? key, + required ValueChanged? onDateSelected, + required DateTime? selectedDate, + DateFormat? dateFormat, + TextStyle? dateTextStyle, + InputDecoration? decoration, + bool? enabled, + DateTime? firstDate, + DateTime? initialDate, + DatePickerMode? initialDatePickerMode, + DatePickerEntryMode initialEntryMode, + DateTime? lastDate, + DateTimeFieldPickerMode mode, + bool use24hFormat, +}); /// [DateTimeField] /// /// Shows an [_InputDropdown] that'll trigger [DateTimeField._selectDate] whenever the user /// clicks on it ! The date picker is **platform responsive** (ios date picker style for ios, ...) class DateTimeField extends StatelessWidget { + // Note: This should match the definition of the [DateTimeFieldCreator] DateTimeField({ Key? key, required this.onDateSelected, @@ -106,22 +126,11 @@ class DateTimeField extends StatelessWidget { } if (Theme.of(context).platform == TargetPlatform.iOS) { - showModalBottomSheet( - context: context, - builder: (BuildContext builder) { - return SizedBox( - height: _kCupertinoDatePickerHeight, - child: CupertinoDatePicker( - mode: _cupertinoModeFromPickerMode(mode), - onDateTimeChanged: onDateSelected!, - initialDateTime: initialDateTime, - minimumDate: firstDate, - maximumDate: lastDate, - use24hFormat: use24hFormat, - ), - ); - }, - ); + final DateTime? _selectedDateTime = + await showCupertinoPicker(context, initialDateTime); + if (_selectedDateTime != null) { + onDateSelected!(_selectedDateTime); + } } else { DateTime _selectedDateTime = initialDateTime; @@ -132,14 +141,8 @@ class DateTimeField extends StatelessWidget { ]; if (modesWithDate.contains(mode)) { - final DateTime? _selectedDate = await showDatePicker( - context: context, - initialDatePickerMode: initialDatePickerMode!, - initialDate: initialDateTime, - initialEntryMode: initialEntryMode, - firstDate: firstDate, - lastDate: lastDate, - ); + final DateTime? _selectedDate = + await showMaterialDatePicker(context, initialDateTime); if (_selectedDate != null) { _selectedDateTime = _selectedDate; @@ -155,10 +158,8 @@ class DateTimeField extends StatelessWidget { ]; if (modesWithTime.contains(mode)) { - final TimeOfDay? _selectedTime = await showTimePicker( - initialTime: TimeOfDay.fromDateTime(initialDateTime), - context: context, - ); + final TimeOfDay? _selectedTime = + await showMaterialTimePicker(context, initialDateTime); if (_selectedTime != null) { _selectedDateTime = DateTime( @@ -175,6 +176,60 @@ class DateTimeField extends StatelessWidget { } } + /// Launches the Material time picker by invoking [showTimePicker]. + /// Can be @[override]n to allow further customization of the picker options + Future showMaterialTimePicker( + BuildContext context, + DateTime initialDateTime, + ) async { + return showTimePicker( + initialTime: TimeOfDay.fromDateTime(initialDateTime), + context: context, + ); + } + + /// Launches the Material time picker by invoking [showDatePicker]. + /// Can be @[override]n to allow further customization of the picker options + Future showMaterialDatePicker( + BuildContext context, + DateTime initialDateTime, + ) { + return showDatePicker( + context: context, + initialDatePickerMode: initialDatePickerMode!, + initialDate: initialDateTime, + initialEntryMode: initialEntryMode, + firstDate: firstDate, + lastDate: lastDate, + ); + } + + /// Launches the [CupertinoDatePicker] within a [showModalBottomSheet]. + /// Can be @[override]n to allow further customization of the picker options + Future showCupertinoPicker( + BuildContext context, + DateTime initialDateTime, + ) async { + DateTime? pickedDate; + await showModalBottomSheet( + context: context, + builder: (BuildContext builder) { + return SizedBox( + height: kCupertinoDatePickerHeight, + child: CupertinoDatePicker( + mode: cupertinoModeFromPickerMode(mode), + onDateTimeChanged: (DateTime dt) => pickedDate = dt, + initialDateTime: initialDateTime, + minimumDate: firstDate, + maximumDate: lastDate, + use24hFormat: use24hFormat, + ), + ); + }, + ); + return pickedDate; + } + @override Widget build(BuildContext context) { String? text; @@ -203,7 +258,7 @@ enum DateTimeFieldPickerMode { time, date, dateAndTime } /// Returns the [CupertinoDatePickerMode] corresponding to the selected /// [DateTimeFieldPickerMode]. This exists to prevent redundancy in the [DateTimeField] /// widget parameters. -CupertinoDatePickerMode _cupertinoModeFromPickerMode( +CupertinoDatePickerMode cupertinoModeFromPickerMode( DateTimeFieldPickerMode mode) { switch (mode) { case DateTimeFieldPickerMode.time: diff --git a/lib/src/form_field.dart b/lib/src/form_field.dart index bae689a..b4fec16 100644 --- a/lib/src/form_field.dart +++ b/lib/src/form_field.dart @@ -1,5 +1,4 @@ import 'package:date_field/src/field.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; @@ -31,6 +30,7 @@ class DateTimeFormField extends FormField { DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar, DatePickerMode initialDatePickerMode = DatePickerMode.day, DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime, + DateTimeFieldCreator fieldCreator = DateTimeField.new, }) : super( key: key, initialValue: initialValue, @@ -54,7 +54,7 @@ class DateTimeFormField extends FormField { field.didChange(value); } - return DateTimeField( + return fieldCreator( firstDate: firstDate, initialDate: initialDate, lastDate: lastDate, diff --git a/pubspec.lock b/pubspec.lock deleted file mode 100644 index e680b3d..0000000 --- a/pubspec.lock +++ /dev/null @@ -1,71 +0,0 @@ -# Generated by pub -# See https://dart.dev/tools/pub/glossary#lockfile -packages: - characters: - dependency: transitive - description: - name: characters - url: "https://pub.dartlang.org" - source: hosted - version: "1.2.0" - clock: - dependency: transitive - description: - name: clock - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.0" - collection: - dependency: transitive - description: - name: collection - url: "https://pub.dartlang.org" - source: hosted - version: "1.15.0" - flutter: - dependency: "direct main" - description: flutter - source: sdk - version: "0.0.0" - intl: - dependency: "direct main" - description: - name: intl - url: "https://pub.dartlang.org" - source: hosted - version: "0.17.0" - meta: - dependency: transitive - description: - name: meta - url: "https://pub.dartlang.org" - source: hosted - version: "1.7.0" - path: - dependency: transitive - description: - name: path - url: "https://pub.dartlang.org" - source: hosted - version: "1.8.1" - sky_engine: - dependency: transitive - description: flutter - source: sdk - version: "0.0.99" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" - vector_math: - dependency: transitive - description: - name: vector_math - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.1" -sdks: - dart: ">=2.14.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index a5efcdb..b38bf3f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: date_field description: A widget in the form of a field that lets people choose a date, a time or both. -version: 2.1.3 -homepage: 'https://github.com/GaspardMerten/date_field' +version: 3.0.0 +homepage: 'https://github.com/axrs/date_field' environment: - sdk: '>=2.12.0 <3.0.0' + sdk: '>=2.15.0 <3.0.0' dependencies: flutter: