@@ -2,11 +2,11 @@ import 'dart:async';
2
2
import 'dart:ui' as ui;
3
3
4
4
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart' ;
5
+ import 'package:flutter/foundation.dart' ;
5
6
import 'package:flutter/material.dart' ;
6
7
import 'package:flutter/services.dart' ;
7
8
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart' ;
8
9
import 'package:flutter_form_builder/flutter_form_builder.dart' ;
9
- import 'package:flutter_form_builder/src/utils.dart' ;
10
10
import 'package:intl/intl.dart' ;
11
11
12
12
enum InputType { date, time, both }
@@ -301,13 +301,9 @@ class _FormBuilderDateTimePickerState
301
301
void initState () {
302
302
super .initState ();
303
303
_textFieldController = widget.controller ?? TextEditingController ();
304
- }
305
-
306
- @override
307
- void didInitState () {
308
- super .didInitState ();
304
+ final initVal = initialValue;
309
305
_textFieldController.text =
310
- initialValue == null ? '' : dateFormat.format (initialValue );
306
+ initVal == null ? '' : dateFormat.format (initVal );
311
307
}
312
308
313
309
@override
@@ -328,17 +324,27 @@ class _FormBuilderDateTimePickerState
328
324
329
325
DateFormat _getDefaultDateTimeFormat () {
330
326
final appLocale = widget.locale ?? Localizations .localeOf (context);
327
+ final appLocaleCode = appLocale.toString ();
331
328
switch (widget.inputType) {
332
329
case InputType .time:
333
- return DateFormat .Hm (appLocale. toString () );
330
+ return DateFormat .Hm (appLocaleCode );
334
331
case InputType .date:
335
- return DateFormat .yMd (appLocale. toString () );
332
+ return DateFormat .yMd (appLocaleCode );
336
333
case InputType .both:
337
334
default :
338
- return DateFormat .yMd (appLocale. toString () ).add_Hms ();
335
+ return DateFormat .yMd (appLocaleCode ).add_Hms ();
339
336
}
340
337
}
341
338
339
+ LocaleType _localeType () {
340
+ final locale = widget.locale ?? Localizations .localeOf (context);
341
+ final languageCode = locale.languageCode;
342
+ return LocaleType .values.firstWhere (
343
+ (_) => languageCode == describeEnum (_),
344
+ orElse: () => null ,
345
+ );
346
+ }
347
+
342
348
Future <DateTime > onShowPicker (
343
349
BuildContext context, DateTime currentValue) async {
344
350
currentValue = value;
@@ -380,9 +386,7 @@ class _FormBuilderDateTimePickerState
380
386
minTime: widget.firstDate,
381
387
maxTime: widget.lastDate,
382
388
currentTime: currentValue,
383
- locale: enumValueFromString (
384
- (widget.locale ?? Localizations .localeOf (context))? .languageCode,
385
- LocaleType .values),
389
+ locale: _localeType (),
386
390
theme: widget.theme,
387
391
onCancel: widget.onCancel,
388
392
onConfirm: widget.onConfirm,
@@ -433,10 +437,7 @@ class _FormBuilderDateTimePickerState
433
437
showTitleActions: true ,
434
438
currentTime: currentValue,
435
439
showSecondsColumn: false ,
436
- locale: enumValueFromString (
437
- (widget.locale ?? Localizations .localeOf (context))
438
- ? .languageCode,
439
- LocaleType .values),
440
+ locale: _localeType (),
440
441
).then (
441
442
(result) {
442
443
return TimeOfDay .fromDateTime (result ?? currentValue);
@@ -447,9 +448,7 @@ class _FormBuilderDateTimePickerState
447
448
context,
448
449
showTitleActions: true ,
449
450
currentTime: currentValue,
450
- locale: enumValueFromString (
451
- (widget.locale ?? Localizations .localeOf (context))? .languageCode,
452
- LocaleType .values),
451
+ locale: _localeType (),
453
452
).then (
454
453
(result) {
455
454
return TimeOfDay .fromDateTime (result ?? currentValue);
@@ -487,7 +486,7 @@ class _FormBuilderDateTimePickerState
487
486
}
488
487
489
488
@override
490
- void patchValue (dynamic val) {
489
+ void patchValue (DateTime val) {
491
490
super .patchValue (val);
492
491
_textFieldController.text = val == null ? '' : dateFormat.format (val);
493
492
}
0 commit comments