Formatting with dart-fm

This commit is contained in:
Gaspard Merten 2020-07-15 18:55:02 +02:00
parent c520484ef8
commit 97da6f5955
3 changed files with 71 additions and 70 deletions

View file

@ -27,4 +27,8 @@
## 0.2.1 ## 0.2.1
* Adding support for Flutter web * Adding support for Flutter web
## 0.2.2
* Auto-formatting with dart-fm to meet pub.dev requirements

View file

@ -12,51 +12,51 @@ import 'package:intl/intl.dart';
/// pass a [GlobalKey] to the constructor and use [GlobalKey.currentState] to /// pass a [GlobalKey] to the constructor and use [GlobalKey.currentState] to
/// save or reset the form field. /// save or reset the form field.
class DateFormField extends FormField<DateTime> { class DateFormField extends FormField<DateTime> {
DateFormField(
{Key key,
FormFieldSetter<DateTime> onSaved,
FormFieldValidator<DateTime> validator,
DateTime initialValue,
bool autovalidate = false,
bool enabled = true,
this.onDateSelected,
this.firstDate,
this.lastDate,
this.label = 'Select date',
this.dateFormat,
this.decoration,
this.initialDatePickerMode = DatePickerMode.day})
: super(
key: key,
initialValue: initialValue,
onSaved: onSaved,
validator: validator,
autovalidate: autovalidate,
enabled: enabled,
builder: (FormFieldState<DateTime> field) {
final _DateFormFieldState state = field;
DateFormField({ void onChangedHandler(DateTime value) {
Key key, if (onDateSelected != null) {
FormFieldSetter<DateTime> onSaved, onDateSelected(value);
FormFieldValidator<DateTime> validator, }
DateTime initialValue, field.didChange(value);
bool autovalidate = false, }
bool enabled = true,
this.onDateSelected,
this.firstDate,
this.lastDate,
this.label = 'Select date',
this.dateFormat,
this.decoration,
this.initialDatePickerMode = DatePickerMode.day
}) : super(
key: key,
initialValue: initialValue,
onSaved: onSaved,
validator: validator,
autovalidate: autovalidate,
enabled: enabled,
builder: (FormFieldState<DateTime> field) {
final _DateFormFieldState state = field;
void onChangedHandler(DateTime value) { return DateField(
if (onDateSelected != null) { label: label,
onDateSelected(value); firstDate: firstDate,
} lastDate: lastDate,
field.didChange(value); decoration: decoration,
} initialDatePickerMode: initialDatePickerMode,
return DateField( dateFormat: dateFormat,
label: label, errorText: state.errorText,
firstDate: firstDate, onDateSelected: onChangedHandler,
lastDate: lastDate, selectedDate: state.value,
decoration: decoration, enabled: enabled,
initialDatePickerMode: initialDatePickerMode, );
dateFormat: dateFormat, },
errorText: state.errorText, );
onDateSelected: onChangedHandler,
selectedDate: state.value,
enabled: enabled,
);
},
);
/// (optional) A callback that will be triggered whenever a new /// (optional) A callback that will be triggered whenever a new
/// DateTime is selected /// DateTime is selected
@ -80,7 +80,6 @@ class DateFormField extends FormField<DateTime> {
/// (optional) Let you choose the [DatePickerMode] for the date picker! (default is [DatePickerMode.day] /// (optional) Let you choose the [DatePickerMode] for the date picker! (default is [DatePickerMode.day]
final DatePickerMode initialDatePickerMode; final DatePickerMode initialDatePickerMode;
@override @override
_DateFormFieldState createState() => _DateFormFieldState(); _DateFormFieldState createState() => _DateFormFieldState();
} }
@ -92,7 +91,6 @@ class _DateFormFieldState extends FormFieldState<DateTime> {}
/// Shows an [_InputDropdown] that'll trigger [DateField._selectDate] whenever the user /// Shows an [_InputDropdown] that'll trigger [DateField._selectDate] whenever the user
/// 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 DateField extends StatelessWidget { class DateField extends StatelessWidget {
/// Default constructor /// Default constructor
const DateField({ const DateField({
@required this.onDateSelected, @required this.onDateSelected,
@ -155,9 +153,7 @@ class DateField extends StatelessWidget {
); );
}, },
); );
} } else {
else {
final DateTime _selectedDate = await showDatePicker( final DateTime _selectedDate = await showDatePicker(
context: context, context: context,
initialDatePickerMode: initialDatePickerMode, initialDatePickerMode: initialDatePickerMode,
@ -183,9 +179,11 @@ class DateField extends StatelessWidget {
label: text == null ? null : label, label: text == null ? null : label,
errorText: errorText, errorText: errorText,
decoration: decoration, decoration: decoration,
onPressed: enabled ? () { onPressed: enabled
_selectDate(context); ? () {
} : null, _selectDate(context);
}
: null,
); );
} }
} }
@ -197,16 +195,15 @@ class DateField extends StatelessWidget {
/// It does not show any popup menu, it'll just trigger onPressed whenever the /// It does not show any popup menu, it'll just trigger onPressed whenever the
/// 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.label, this.label,
this.decoration, this.decoration,
this.textStyle, this.textStyle,
this.onPressed, this.onPressed,
this.errorText, this.errorText,
}) : }) : assert(text != null),
assert(text != null),
super(key: key); super(key: key);
/// The label to display for the field (default is 'Select date') /// The label to display for the field (default is 'Select date')
@ -235,13 +232,13 @@ class _InputDropdown extends StatelessWidget {
inkwellBorderRadius = BorderRadius.circular(8); inkwellBorderRadius = BorderRadius.circular(8);
} }
final InputDecoration effectiveDecoration = decoration?.copyWith( final InputDecoration effectiveDecoration =
errorText: errorText decoration?.copyWith(errorText: errorText) ??
) ?? InputDecoration( InputDecoration(
labelText: label, labelText: label,
errorText: errorText, errorText: errorText,
suffixIcon: Icon(Icons.arrow_drop_down), suffixIcon: Icon(Icons.arrow_drop_down),
).applyDefaults(Theme.of(context).inputDecorationTheme); ).applyDefaults(Theme.of(context).inputDecorationTheme);
return Material( return Material(
color: Colors.transparent, color: Colors.transparent,

View file

@ -1,6 +1,6 @@
name: date_field name: date_field
description: Contains DateField and DateFormField which allows the user to pick a DateTime from an input field! description: Contains DateField and DateFormField which allows the user to pick a DateTime from an input field!
version: 0.2.1 version: 0.2.2
homepage: 'https://github.com/GaspardMerten/date_field' homepage: 'https://github.com/GaspardMerten/date_field'
environment: environment: