From fa2bdd1d4e23fa0ff22c4c1bd2ef4eb709951945 Mon Sep 17 00:00:00 2001 From: Gaspard Merten Date: Thu, 11 Mar 2021 19:43:23 +0100 Subject: [PATCH] Migrating to null safety --- CHANGELOG.md | 4 ++ example/pubspec.lock | 42 +++++++++--------- lib/date_field.dart | 101 +++++++++++++++++++++---------------------- pubspec.lock | 23 ++++++---- pubspec.yaml | 6 +-- 5 files changed, 93 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 104b285..d1fd278 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,3 +87,7 @@ Breaking change: * Removing unused variables * Improving description + +##2.0.0 + +* Migrating to null-safety \ No newline at end of file diff --git a/example/pubspec.lock b/example/pubspec.lock index 6430bb5..84d66c5 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,42 +7,42 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0-nullsafety.3" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.3" + version: "2.1.0" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.5" + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" clock: dependency: transitive description: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.3" + version: "1.1.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0-nullsafety.5" + version: "1.15.0" cupertino_icons: dependency: "direct main" description: @@ -56,14 +56,14 @@ packages: path: ".." relative: true source: path - version: "1.0.4" + version: "1.0.5" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -80,28 +80,28 @@ packages: name: intl url: "https://pub.dartlang.org" source: hosted - version: "0.16.1" + version: "0.17.0" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10-nullsafety.3" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.6" + version: "1.3.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.3" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -113,55 +113,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.4" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.10.0-nullsafety.6" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.3" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.3" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19-nullsafety.6" + version: "0.2.19" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.5" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.5" + version: "2.1.0" sdks: - dart: ">=2.12.0-0.0 <3.0.0" + dart: ">=2.12.0 <3.0.0" diff --git a/lib/date_field.dart b/lib/date_field.dart index a0ecd8b..b6ccc05 100644 --- a/lib/date_field.dart +++ b/lib/date_field.dart @@ -18,18 +18,18 @@ const double _kCupertinoDatePickerHeight = 216; /// save or reset the form field. class DateTimeFormField extends FormField { DateTimeFormField({ - Key key, - FormFieldSetter onSaved, - FormFieldValidator validator, - DateTime initialValue, - AutovalidateMode autovalidateMode, + Key? key, + FormFieldSetter? onSaved, + FormFieldValidator? validator, + DateTime? initialValue, + AutovalidateMode? autovalidateMode, bool enabled = true, - TextStyle dateTextStyle, - DateFormat dateFormat, - DateTime firstDate, - DateTime lastDate, - ValueChanged onDateSelected, - InputDecoration decoration, + TextStyle? dateTextStyle, + DateFormat? dateFormat, + DateTime? firstDate, + DateTime? lastDate, + ValueChanged? onDateSelected, + InputDecoration? decoration, DatePickerMode initialDatePickerMode = DatePickerMode.day, DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime, }) : super( @@ -40,8 +40,6 @@ class DateTimeFormField extends FormField { autovalidateMode: autovalidateMode, enabled: enabled, builder: (FormFieldState field) { - final _DateFormFieldState state = field; - // Theme defaults are applied inside the _InputDropdown widget final InputDecoration _decorationWithThemeDefaults = decoration ?? const InputDecoration(); @@ -64,7 +62,7 @@ class DateTimeFormField extends FormField { initialDatePickerMode: initialDatePickerMode, dateFormat: dateFormat, onDateSelected: onChangedHandler, - selectedDate: state.value, + selectedDate: field.value, enabled: enabled, mode: mode, dateTextStyle: dateTextStyle, @@ -84,31 +82,31 @@ class _DateFormFieldState extends FormFieldState {} /// clicks on it ! The date picker is **platform responsive** (ios date picker style for ios, ...) class DateTimeField extends StatelessWidget { DateTimeField({ - Key key, - @required this.onDateSelected, - @required this.selectedDate, + Key? key, + required this.onDateSelected, + required this.selectedDate, this.initialDatePickerMode = DatePickerMode.day, this.decoration, this.enabled = true, this.mode = DateTimeFieldPickerMode.dateAndTime, this.dateTextStyle, - DateTime firstDate, - DateTime lastDate, - DateFormat dateFormat, + DateTime? firstDate, + DateTime? lastDate, + DateFormat? dateFormat, }) : dateFormat = dateFormat ?? getDateFormatFromDateFieldPickerMode(mode), firstDate = firstDate ?? _kDefaultFirstSelectableDate, lastDate = lastDate ?? _kDefaultLastSelectableDate, super(key: key); DateTimeField.time({ - Key key, + Key? key, this.onDateSelected, this.selectedDate, this.decoration, this.enabled, this.dateTextStyle, - DateTime firstDate, - DateTime lastDate, + DateTime? firstDate, + DateTime? lastDate, }) : initialDatePickerMode = null, mode = DateTimeFieldPickerMode.time, dateFormat = DateFormat.jm(), @@ -117,10 +115,10 @@ class DateTimeField extends StatelessWidget { super(key: key); /// Callback for whenever the user selects a [DateTime] - final ValueChanged onDateSelected; + final ValueChanged? onDateSelected; /// The current selected date to display inside the field - final DateTime selectedDate; + final DateTime? selectedDate; /// The first date that the user can select (default is 1900) final DateTime firstDate; @@ -129,22 +127,22 @@ class DateTimeField extends StatelessWidget { final DateTime lastDate; /// Let you choose the [DatePickerMode] for the date picker! (default is [DatePickerMode.day] - final DatePickerMode initialDatePickerMode; + final DatePickerMode? initialDatePickerMode; /// Custom [InputDecoration] for the [InputDecorator] widget - final InputDecoration decoration; + final InputDecoration? decoration; /// How to display the [DateTime] for the user (default is [DateFormat.yMMMD]) final DateFormat dateFormat; /// Whether the field is usable. If false the user won't be able to select any date - final bool enabled; + final bool? enabled; /// Whether to ask the user to pick only the date, the time or both. final DateTimeFieldPickerMode mode; /// [TextStyle] of the selected date inside the field. - final TextStyle dateTextStyle; + final TextStyle? dateTextStyle; /// Shows a dialog asking the user to pick a date ! Future _selectDate(BuildContext context) async { @@ -158,7 +156,7 @@ class DateTimeField extends StatelessWidget { height: _kCupertinoDatePickerHeight, child: CupertinoDatePicker( mode: _cupertinoModeFromPickerMode(mode), - onDateTimeChanged: onDateSelected, + onDateTimeChanged: onDateSelected!, initialDateTime: initialDateTime, minimumDate: firstDate, maximumDate: lastDate, @@ -171,9 +169,9 @@ class DateTimeField extends StatelessWidget { if ([DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.date] .contains(mode)) { - final DateTime _selectedDate = await showDatePicker( + final DateTime? _selectedDate = await showDatePicker( context: context, - initialDatePickerMode: initialDatePickerMode, + initialDatePickerMode: initialDatePickerMode!, initialDate: initialDateTime, firstDate: firstDate, lastDate: lastDate, @@ -188,7 +186,7 @@ class DateTimeField extends StatelessWidget { if ([DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.time] .contains(mode)) { - final TimeOfDay _selectedTime = await showTimePicker( + final TimeOfDay? _selectedTime = await showTimePicker( initialTime: TimeOfDay.fromDateTime(initialDateTime), context: context, ); @@ -203,39 +201,41 @@ class DateTimeField extends StatelessWidget { } } - onDateSelected(_selectedDateTime); + onDateSelected!(_selectedDateTime); } } @override Widget build(BuildContext context) { - String text; + String? text; - if (selectedDate != null) text = dateFormat.format(selectedDate); + if (selectedDate != null) text = dateFormat.format(selectedDate!); - TextStyle textStyle; + TextStyle? textStyle; if (text == null) { - textStyle = decoration.hintStyle ?? + textStyle = decoration!.hintStyle ?? Theme.of(context).inputDecorationTheme.hintStyle; } else { textStyle = dateTextStyle ?? dateTextStyle; } - final bool shouldDisplayLabelText = (text ?? decoration.hintText) != null; + final bool shouldDisplayLabelText = (text ?? decoration!.hintText) != null; - InputDecoration effectiveDecoration = decoration; + InputDecoration? effectiveDecoration = decoration; if (!shouldDisplayLabelText) { - effectiveDecoration = effectiveDecoration.copyWith(labelText: ''); + effectiveDecoration = effectiveDecoration!.copyWith(labelText: ''); } return _InputDropdown( - text: - text ?? decoration.hintText ?? decoration.labelText ?? 'Select date', + text: text ?? + decoration!.hintText ?? + decoration!.labelText ?? + 'Select date', textStyle: textStyle, decoration: effectiveDecoration, - onPressed: enabled ? () => _selectDate(context) : null, + onPressed: enabled! ? () => _selectDate(context) : null, ); } } @@ -279,25 +279,24 @@ DateFormat getDateFormatFromDateFieldPickerMode(DateTimeFieldPickerMode mode) { /// user does click on it ! class _InputDropdown extends StatelessWidget { const _InputDropdown({ - Key key, - @required this.text, + Key? key, + required this.text, this.decoration, this.textStyle, this.onPressed, - }) : assert(text != null), - super(key: key); + }) : super(key: key); /// The text that should be displayed inside the field final String text; /// Custom [InputDecoration] for the [InputDecorator] widget - final InputDecoration decoration; + final InputDecoration? decoration; /// TextStyle for the field - final TextStyle textStyle; + final TextStyle? textStyle; /// Callbacks triggered whenever the user presses on the field! - final VoidCallback onPressed; + final VoidCallback? onPressed; @override Widget build(BuildContext context) { diff --git a/pubspec.lock b/pubspec.lock index 6bfcd5a..967afee 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,14 +7,21 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.5" + version: "1.1.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-nullsafety.5" + version: "1.15.0" flutter: dependency: "direct main" description: flutter @@ -26,21 +33,21 @@ packages: name: intl url: "https://pub.dartlang.org" source: hosted - version: "0.16.1" + version: "0.17.0" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.6" + version: "1.3.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -52,13 +59,13 @@ packages: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.5" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.5" + version: "2.1.0" sdks: - dart: ">=2.12.0-0 <3.0.0" + dart: ">=2.12.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 09fb687..ba80505 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,14 +1,14 @@ name: date_field description: A widget in the form of a field that lets people choose a date, a time or both. -version: 1.0.5 +version: 2.0.0 homepage: 'https://github.com/GaspardMerten/date_field' environment: - sdk: ">=2.1.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: sdk: flutter - intl: ">=0.2.7 <5.0.0" + intl: ^0.17.0