Skip to content

Commit 755eed0

Browse files
authored
Date time picker 548 (#551)
Fix: Added DateTimeField.onChange hook to resolve #548 * Fix: Canceling a DateTimePicker now behaves correctly * Fix: Locale override for DateTimePicker * Fix: FormBuilderDateRangePicker.initState * Example: Add DateTimePicker with a Spanish locale to prove locale override works
1 parent 4a9d474 commit 755eed0

File tree

3 files changed

+54
-54
lines changed

3 files changed

+54
-54
lines changed

example/lib/sources/complete_form.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ class CompleteFormState extends State<CompleteForm> {
159159
),
160160
initialTime: TimeOfDay(hour: 8, minute: 0),
161161
pickerType: PickerType.cupertino,
162+
//locale: Locale.fromSubtags(languageCode: 'fr'),
163+
),
164+
FormBuilderDateTimePicker(
165+
name: 'date_es',
166+
initialValue: DateTime.now(),
167+
inputType: InputType.both,
168+
decoration: const InputDecoration(
169+
labelText: 'Hora de la cita',
170+
),
171+
initialTime: TimeOfDay(hour: 8, minute: 0),
172+
pickerType: PickerType.cupertino,
173+
locale: Locale.fromSubtags(languageCode: 'es'),
162174
),
163175
FormBuilderDateRangePicker(
164176
name: 'date_range',

lib/src/fields/form_builder_date_range_picker.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ class FormBuilderDateRangePickerState
190190

191191
@override
192192
void initState() {
193+
super.initState();
193194
_effectiveController =
194195
widget.controller ?? TextEditingController(text: _valueToText());
195196
effectiveFocusNode.addListener(_handleFocus);
196-
super.initState();
197197
}
198198

199199
@override

lib/src/fields/form_builder_date_time_picker.dart

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class FormBuilderDateTimePicker extends FormBuilderField<DateTime> {
236236

237237
return DateTimeField(
238238
initialValue: state.initialValue,
239-
format: state.dateFormat,
239+
format: state._dateFormat,
240240
validator: validator,
241241
onShowPicker: state.onShowPicker,
242242
autovalidate: autovalidateMode != AutovalidateMode.disabled &&
@@ -273,6 +273,7 @@ class FormBuilderDateTimePicker extends FormBuilderField<DateTime> {
273273
strutStyle: strutStyle,
274274
textCapitalization: textCapitalization,
275275
textInputAction: textInputAction,
276+
onChanged: (val) => state.didChange(val),
276277
);
277278
},
278279
);
@@ -288,17 +289,16 @@ class _FormBuilderDateTimePickerState
288289
extends FormBuilderFieldState<FormBuilderDateTimePicker, DateTime> {
289290
TextEditingController _textFieldController;
290291

291-
// DateTime stateCurrentValue;
292-
293-
DateFormat get dateFormat => widget.format ?? _getDefaultDateTimeFormat();
292+
DateFormat _dateFormat;
294293

295294
@override
296295
void initState() {
297296
super.initState();
298297
_textFieldController = widget.controller ?? TextEditingController();
298+
_dateFormat = widget.format ?? _getDefaultDateTimeFormat();
299299
final initVal = initialValue;
300300
_textFieldController.text =
301-
initVal == null ? '' : dateFormat.format(initVal);
301+
initVal == null ? '' : _dateFormat.format(initVal);
302302
}
303303

304304
@override
@@ -318,25 +318,24 @@ class _FormBuilderDateTimePickerState
318318
}*/
319319

320320
DateFormat _getDefaultDateTimeFormat() {
321-
final appLocale = widget.locale ?? Localizations.localeOf(context);
322-
final appLocaleCode = appLocale.toString();
321+
final languageCode = widget.locale?.languageCode;
323322
switch (widget.inputType) {
324323
case InputType.time:
325-
return DateFormat.Hm(appLocaleCode);
324+
return DateFormat.Hm(languageCode);
326325
case InputType.date:
327-
return DateFormat.yMd(appLocaleCode);
326+
return DateFormat.yMd(languageCode);
328327
case InputType.both:
329328
default:
330-
return DateFormat.yMd(appLocaleCode).add_Hms();
329+
return DateFormat.yMd(languageCode).add_Hms();
331330
}
332331
}
333332

334333
LocaleType _localeType() {
335-
final locale = widget.locale ?? Localizations.localeOf(context);
336-
final languageCode = locale.languageCode;
334+
final shortLocaleCode = widget.locale?.languageCode ??
335+
Intl.shortLocale(Intl.getCurrentLocale());
337336
return LocaleType.values.firstWhere(
338-
(_) => languageCode == describeEnum(_),
339-
orElse: () => null,
337+
(_) => shortLocaleCode == describeEnum(_),
338+
orElse: () => LocaleType.en,
340339
);
341340
}
342341

@@ -346,12 +345,11 @@ class _FormBuilderDateTimePickerState
346345
DateTime newValue;
347346
switch (widget.inputType) {
348347
case InputType.date:
349-
newValue = await _showDatePicker(context, currentValue) ?? currentValue;
348+
newValue = await _showDatePicker(context, currentValue);
350349
break;
351350
case InputType.time:
352351
final newTime = await _showTimePicker(context, currentValue);
353-
newValue =
354-
newTime != null ? DateTimeField.convert(newTime) : currentValue;
352+
newValue = null != newTime ? DateTimeField.convert(newTime) : null;
355353
break;
356354
case InputType.both:
357355
final date = await _showDatePicker(context, currentValue);
@@ -421,36 +419,30 @@ class _FormBuilderDateTimePickerState
421419
}
422420

423421
Future<TimeOfDay> _showTimePicker(
424-
BuildContext context, DateTime currentValue) {
422+
BuildContext context, DateTime currentValue) async {
425423
if (widget.timePicker != null) {
426424
return widget.timePicker(context);
427425
} else {
428426
if (widget.pickerType == PickerType.cupertino) {
429-
if (widget.alwaysUse24HourFormat) {
430-
return DatePicker.showTimePicker(
431-
context,
432-
showTitleActions: true,
433-
currentTime: currentValue,
434-
showSecondsColumn: false,
435-
locale: _localeType(),
436-
).then(
437-
(result) {
438-
return TimeOfDay.fromDateTime(result ?? currentValue);
439-
},
440-
);
441-
}
442-
return DatePicker.showTime12hPicker(
443-
context,
444-
showTitleActions: true,
445-
currentTime: currentValue,
446-
locale: _localeType(),
447-
).then(
448-
(result) {
449-
return TimeOfDay.fromDateTime(result ?? currentValue);
450-
},
451-
);
427+
final timePicker = widget.alwaysUse24HourFormat
428+
? DatePicker.showTimePicker(
429+
context,
430+
showTitleActions: true,
431+
currentTime: currentValue,
432+
showSecondsColumn: false,
433+
locale: _localeType(),
434+
)
435+
: DatePicker.showTime12hPicker(
436+
context,
437+
showTitleActions: true,
438+
currentTime: currentValue,
439+
locale: _localeType(),
440+
);
441+
final timePickerResult = await timePicker;
442+
final newDateTime = timePickerResult ?? currentValue;
443+
return null != newDateTime ? TimeOfDay.fromDateTime(newDateTime) : null;
452444
}
453-
return showTimePicker(
445+
final timePickerResult = await showTimePicker(
454446
context: context,
455447
initialTime: currentValue != null
456448
? TimeOfDay.fromDateTime(currentValue)
@@ -459,7 +451,8 @@ class _FormBuilderDateTimePickerState
459451
(BuildContext context, Widget child) {
460452
return MediaQuery(
461453
data: MediaQuery.of(context).copyWith(
462-
alwaysUse24HourFormat: widget.alwaysUse24HourFormat),
454+
alwaysUse24HourFormat: widget.alwaysUse24HourFormat,
455+
),
463456
child: child,
464457
);
465458
},
@@ -469,20 +462,15 @@ class _FormBuilderDateTimePickerState
469462
helpText: widget.helpText,
470463
confirmText: widget.confirmText,
471464
cancelText: widget.cancelText,
472-
).then(
473-
(result) {
474-
return result ??
475-
(currentValue != null
476-
? TimeOfDay.fromDateTime(currentValue)
477-
: null);
478-
},
479465
);
466+
return timePickerResult ??
467+
(currentValue != null ? TimeOfDay.fromDateTime(currentValue) : null);
480468
}
481469
}
482470

483471
@override
484-
void patchValue(DateTime val) {
485-
super.patchValue(val);
486-
_textFieldController.text = val == null ? '' : dateFormat.format(val);
472+
void didChange(DateTime val) {
473+
super.didChange(val);
474+
_textFieldController.text = val == null ? '' : _dateFormat.format(val);
487475
}
488476
}

0 commit comments

Comments
 (0)