Migrating to null safety

This commit is contained in:
Gaspard Merten 2021-03-11 19:43:23 +01:00
parent cee1bb3f82
commit fa2bdd1d4e
5 changed files with 93 additions and 83 deletions

View file

@ -87,3 +87,7 @@ Breaking change:
* Removing unused variables * Removing unused variables
* Improving description * Improving description
##2.0.0
* Migrating to null-safety

View file

@ -7,42 +7,42 @@ packages:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.5.0-nullsafety.3" version: "2.5.0"
boolean_selector: boolean_selector:
dependency: transitive dependency: transitive
description: description:
name: boolean_selector name: boolean_selector
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.3" version: "2.1.0"
characters: characters:
dependency: transitive dependency: transitive
description: description:
name: characters name: characters
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.5" version: "1.1.0"
charcode: charcode:
dependency: transitive dependency: transitive
description: description:
name: charcode name: charcode
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.3" version: "1.2.0"
clock: clock:
dependency: transitive dependency: transitive
description: description:
name: clock name: clock
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.3" version: "1.1.0"
collection: collection:
dependency: transitive dependency: transitive
description: description:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.15.0-nullsafety.5" version: "1.15.0"
cupertino_icons: cupertino_icons:
dependency: "direct main" dependency: "direct main"
description: description:
@ -56,14 +56,14 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "1.0.4" version: "1.0.5"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
name: fake_async name: fake_async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.3" version: "1.2.0"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -80,28 +80,28 @@ packages:
name: intl name: intl
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.16.1" version: "0.17.0"
matcher: matcher:
dependency: transitive dependency: transitive
description: description:
name: matcher name: matcher
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.10-nullsafety.3" version: "0.12.10"
meta: meta:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.6" version: "1.3.0"
path: path:
dependency: transitive dependency: transitive
description: description:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.3" version: "1.8.0"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -113,55 +113,55 @@ packages:
name: source_span name: source_span
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.4" version: "1.8.0"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
name: stack_trace name: stack_trace
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.10.0-nullsafety.6" version: "1.10.0"
stream_channel: stream_channel:
dependency: transitive dependency: transitive
description: description:
name: stream_channel name: stream_channel
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.3" version: "2.1.0"
string_scanner: string_scanner:
dependency: transitive dependency: transitive
description: description:
name: string_scanner name: string_scanner
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.3" version: "1.1.0"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
name: term_glyph name: term_glyph
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.3" version: "1.2.0"
test_api: test_api:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.19-nullsafety.6" version: "0.2.19"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
name: typed_data name: typed_data
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.5" version: "1.3.0"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
name: vector_math name: vector_math
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.5" version: "2.1.0"
sdks: sdks:
dart: ">=2.12.0-0.0 <3.0.0" dart: ">=2.12.0 <3.0.0"

View file

@ -18,18 +18,18 @@ const double _kCupertinoDatePickerHeight = 216;
/// save or reset the form field. /// save or reset the form field.
class DateTimeFormField extends FormField<DateTime> { class DateTimeFormField extends FormField<DateTime> {
DateTimeFormField({ DateTimeFormField({
Key key, Key? key,
FormFieldSetter<DateTime> onSaved, FormFieldSetter<DateTime>? onSaved,
FormFieldValidator<DateTime> validator, FormFieldValidator<DateTime>? validator,
DateTime initialValue, DateTime? initialValue,
AutovalidateMode autovalidateMode, AutovalidateMode? autovalidateMode,
bool enabled = true, bool enabled = true,
TextStyle dateTextStyle, TextStyle? dateTextStyle,
DateFormat dateFormat, DateFormat? dateFormat,
DateTime firstDate, DateTime? firstDate,
DateTime lastDate, DateTime? lastDate,
ValueChanged<DateTime> onDateSelected, ValueChanged<DateTime>? onDateSelected,
InputDecoration decoration, InputDecoration? decoration,
DatePickerMode initialDatePickerMode = DatePickerMode.day, DatePickerMode initialDatePickerMode = DatePickerMode.day,
DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime,
}) : super( }) : super(
@ -40,8 +40,6 @@ class DateTimeFormField extends FormField<DateTime> {
autovalidateMode: autovalidateMode, autovalidateMode: autovalidateMode,
enabled: enabled, enabled: enabled,
builder: (FormFieldState<DateTime> field) { builder: (FormFieldState<DateTime> field) {
final _DateFormFieldState state = field;
// Theme defaults are applied inside the _InputDropdown widget // Theme defaults are applied inside the _InputDropdown widget
final InputDecoration _decorationWithThemeDefaults = final InputDecoration _decorationWithThemeDefaults =
decoration ?? const InputDecoration(); decoration ?? const InputDecoration();
@ -64,7 +62,7 @@ class DateTimeFormField extends FormField<DateTime> {
initialDatePickerMode: initialDatePickerMode, initialDatePickerMode: initialDatePickerMode,
dateFormat: dateFormat, dateFormat: dateFormat,
onDateSelected: onChangedHandler, onDateSelected: onChangedHandler,
selectedDate: state.value, selectedDate: field.value,
enabled: enabled, enabled: enabled,
mode: mode, mode: mode,
dateTextStyle: dateTextStyle, dateTextStyle: dateTextStyle,
@ -84,31 +82,31 @@ class _DateFormFieldState extends FormFieldState<DateTime> {}
/// clicks on it ! The date picker is **platform responsive** (ios date picker style for ios, ...) /// clicks on it ! The date picker is **platform responsive** (ios date picker style for ios, ...)
class DateTimeField extends StatelessWidget { class DateTimeField extends StatelessWidget {
DateTimeField({ DateTimeField({
Key key, Key? key,
@required this.onDateSelected, required this.onDateSelected,
@required this.selectedDate, required this.selectedDate,
this.initialDatePickerMode = DatePickerMode.day, this.initialDatePickerMode = DatePickerMode.day,
this.decoration, this.decoration,
this.enabled = true, this.enabled = true,
this.mode = DateTimeFieldPickerMode.dateAndTime, this.mode = DateTimeFieldPickerMode.dateAndTime,
this.dateTextStyle, this.dateTextStyle,
DateTime firstDate, DateTime? firstDate,
DateTime lastDate, DateTime? lastDate,
DateFormat dateFormat, DateFormat? dateFormat,
}) : dateFormat = dateFormat ?? getDateFormatFromDateFieldPickerMode(mode), }) : dateFormat = dateFormat ?? getDateFormatFromDateFieldPickerMode(mode),
firstDate = firstDate ?? _kDefaultFirstSelectableDate, firstDate = firstDate ?? _kDefaultFirstSelectableDate,
lastDate = lastDate ?? _kDefaultLastSelectableDate, lastDate = lastDate ?? _kDefaultLastSelectableDate,
super(key: key); super(key: key);
DateTimeField.time({ DateTimeField.time({
Key key, Key? key,
this.onDateSelected, this.onDateSelected,
this.selectedDate, this.selectedDate,
this.decoration, this.decoration,
this.enabled, this.enabled,
this.dateTextStyle, this.dateTextStyle,
DateTime firstDate, DateTime? firstDate,
DateTime lastDate, DateTime? lastDate,
}) : initialDatePickerMode = null, }) : initialDatePickerMode = null,
mode = DateTimeFieldPickerMode.time, mode = DateTimeFieldPickerMode.time,
dateFormat = DateFormat.jm(), dateFormat = DateFormat.jm(),
@ -117,10 +115,10 @@ class DateTimeField extends StatelessWidget {
super(key: key); super(key: key);
/// Callback for whenever the user selects a [DateTime] /// Callback for whenever the user selects a [DateTime]
final ValueChanged<DateTime> onDateSelected; final ValueChanged<DateTime>? onDateSelected;
/// The current selected date to display inside the field /// 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) /// The first date that the user can select (default is 1900)
final DateTime firstDate; final DateTime firstDate;
@ -129,22 +127,22 @@ class DateTimeField extends StatelessWidget {
final DateTime lastDate; final DateTime lastDate;
/// Let you choose the [DatePickerMode] for the date picker! (default is [DatePickerMode.day] /// 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 /// Custom [InputDecoration] for the [InputDecorator] widget
final InputDecoration decoration; final InputDecoration? decoration;
/// How to display the [DateTime] for the user (default is [DateFormat.yMMMD]) /// How to display the [DateTime] for the user (default is [DateFormat.yMMMD])
final DateFormat dateFormat; final DateFormat dateFormat;
/// Whether the field is usable. If false the user won't be able to select any date /// 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. /// Whether to ask the user to pick only the date, the time or both.
final DateTimeFieldPickerMode mode; final DateTimeFieldPickerMode mode;
/// [TextStyle] of the selected date inside the field. /// [TextStyle] of the selected date inside the field.
final TextStyle dateTextStyle; final TextStyle? dateTextStyle;
/// Shows a dialog asking the user to pick a date ! /// Shows a dialog asking the user to pick a date !
Future<void> _selectDate(BuildContext context) async { Future<void> _selectDate(BuildContext context) async {
@ -158,7 +156,7 @@ class DateTimeField extends StatelessWidget {
height: _kCupertinoDatePickerHeight, height: _kCupertinoDatePickerHeight,
child: CupertinoDatePicker( child: CupertinoDatePicker(
mode: _cupertinoModeFromPickerMode(mode), mode: _cupertinoModeFromPickerMode(mode),
onDateTimeChanged: onDateSelected, onDateTimeChanged: onDateSelected!,
initialDateTime: initialDateTime, initialDateTime: initialDateTime,
minimumDate: firstDate, minimumDate: firstDate,
maximumDate: lastDate, maximumDate: lastDate,
@ -171,9 +169,9 @@ class DateTimeField extends StatelessWidget {
if ([DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.date] if ([DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.date]
.contains(mode)) { .contains(mode)) {
final DateTime _selectedDate = await showDatePicker( final DateTime? _selectedDate = await showDatePicker(
context: context, context: context,
initialDatePickerMode: initialDatePickerMode, initialDatePickerMode: initialDatePickerMode!,
initialDate: initialDateTime, initialDate: initialDateTime,
firstDate: firstDate, firstDate: firstDate,
lastDate: lastDate, lastDate: lastDate,
@ -188,7 +186,7 @@ class DateTimeField extends StatelessWidget {
if ([DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.time] if ([DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.time]
.contains(mode)) { .contains(mode)) {
final TimeOfDay _selectedTime = await showTimePicker( final TimeOfDay? _selectedTime = await showTimePicker(
initialTime: TimeOfDay.fromDateTime(initialDateTime), initialTime: TimeOfDay.fromDateTime(initialDateTime),
context: context, context: context,
); );
@ -203,39 +201,41 @@ class DateTimeField extends StatelessWidget {
} }
} }
onDateSelected(_selectedDateTime); onDateSelected!(_selectedDateTime);
} }
} }
@override @override
Widget build(BuildContext context) { 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) { if (text == null) {
textStyle = decoration.hintStyle ?? textStyle = decoration!.hintStyle ??
Theme.of(context).inputDecorationTheme.hintStyle; Theme.of(context).inputDecorationTheme.hintStyle;
} else { } else {
textStyle = dateTextStyle ?? dateTextStyle; 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) { if (!shouldDisplayLabelText) {
effectiveDecoration = effectiveDecoration.copyWith(labelText: ''); effectiveDecoration = effectiveDecoration!.copyWith(labelText: '');
} }
return _InputDropdown( return _InputDropdown(
text: text: text ??
text ?? decoration.hintText ?? decoration.labelText ?? 'Select date', decoration!.hintText ??
decoration!.labelText ??
'Select date',
textStyle: textStyle, textStyle: textStyle,
decoration: effectiveDecoration, 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 ! /// user does click on it !
class _InputDropdown extends StatelessWidget { class _InputDropdown extends StatelessWidget {
const _InputDropdown({ const _InputDropdown({
Key key, Key? key,
@required this.text, required this.text,
this.decoration, this.decoration,
this.textStyle, this.textStyle,
this.onPressed, this.onPressed,
}) : assert(text != null), }) : super(key: key);
super(key: key);
/// The text that should be displayed inside the field /// The text that should be displayed inside the field
final String text; final String text;
/// Custom [InputDecoration] for the [InputDecorator] widget /// Custom [InputDecoration] for the [InputDecorator] widget
final InputDecoration decoration; final InputDecoration? decoration;
/// TextStyle for the field /// TextStyle for the field
final TextStyle textStyle; final TextStyle? textStyle;
/// Callbacks triggered whenever the user presses on the field! /// Callbacks triggered whenever the user presses on the field!
final VoidCallback onPressed; final VoidCallback? onPressed;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {

View file

@ -7,14 +7,21 @@ packages:
name: characters name: characters
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted 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: collection:
dependency: transitive dependency: transitive
description: description:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.15.0-nullsafety.5" version: "1.15.0"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -26,21 +33,21 @@ packages:
name: intl name: intl
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.16.1" version: "0.17.0"
meta: meta:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.6" version: "1.3.0"
path: path:
dependency: transitive dependency: transitive
description: description:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.7.0" version: "1.8.0"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -52,13 +59,13 @@ packages:
name: typed_data name: typed_data
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.5" version: "1.3.0"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
name: vector_math name: vector_math
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.5" version: "2.1.0"
sdks: sdks:
dart: ">=2.12.0-0 <3.0.0" dart: ">=2.12.0 <3.0.0"

View file

@ -1,14 +1,14 @@
name: date_field name: date_field
description: A widget in the form of a field that lets people choose a date, a time or both. 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' homepage: 'https://github.com/GaspardMerten/date_field'
environment: environment:
sdk: ">=2.1.0 <3.0.0" sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
intl: ">=0.2.7 <5.0.0" intl: ^0.17.0