Skip to content

Commit 7b5c8c2

Browse files
authored
Merge pull request #534 from awhitford/date_time_picker
FormBuilderDateTimePicker code refactoring
2 parents 9ebdeed + ecaebe2 commit 7b5c8c2

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

lib/src/fields/form_builder_date_time_picker.dart

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import 'dart:async';
22
import 'dart:ui' as ui;
33

44
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter/services.dart';
78
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
89
import 'package:flutter_form_builder/flutter_form_builder.dart';
9-
import 'package:flutter_form_builder/src/utils.dart';
1010
import 'package:intl/intl.dart';
1111

1212
enum InputType { date, time, both }
@@ -301,13 +301,9 @@ class _FormBuilderDateTimePickerState
301301
void initState() {
302302
super.initState();
303303
_textFieldController = widget.controller ?? TextEditingController();
304-
}
305-
306-
@override
307-
void didInitState() {
308-
super.didInitState();
304+
final initVal = initialValue;
309305
_textFieldController.text =
310-
initialValue == null ? '' : dateFormat.format(initialValue);
306+
initVal == null ? '' : dateFormat.format(initVal);
311307
}
312308

313309
@override
@@ -328,17 +324,27 @@ class _FormBuilderDateTimePickerState
328324

329325
DateFormat _getDefaultDateTimeFormat() {
330326
final appLocale = widget.locale ?? Localizations.localeOf(context);
327+
final appLocaleCode = appLocale.toString();
331328
switch (widget.inputType) {
332329
case InputType.time:
333-
return DateFormat.Hm(appLocale.toString());
330+
return DateFormat.Hm(appLocaleCode);
334331
case InputType.date:
335-
return DateFormat.yMd(appLocale.toString());
332+
return DateFormat.yMd(appLocaleCode);
336333
case InputType.both:
337334
default:
338-
return DateFormat.yMd(appLocale.toString()).add_Hms();
335+
return DateFormat.yMd(appLocaleCode).add_Hms();
339336
}
340337
}
341338

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+
342348
Future<DateTime> onShowPicker(
343349
BuildContext context, DateTime currentValue) async {
344350
currentValue = value;
@@ -380,9 +386,7 @@ class _FormBuilderDateTimePickerState
380386
minTime: widget.firstDate,
381387
maxTime: widget.lastDate,
382388
currentTime: currentValue,
383-
locale: enumValueFromString(
384-
(widget.locale ?? Localizations.localeOf(context))?.languageCode,
385-
LocaleType.values),
389+
locale: _localeType(),
386390
theme: widget.theme,
387391
onCancel: widget.onCancel,
388392
onConfirm: widget.onConfirm,
@@ -433,10 +437,7 @@ class _FormBuilderDateTimePickerState
433437
showTitleActions: true,
434438
currentTime: currentValue,
435439
showSecondsColumn: false,
436-
locale: enumValueFromString(
437-
(widget.locale ?? Localizations.localeOf(context))
438-
?.languageCode,
439-
LocaleType.values),
440+
locale: _localeType(),
440441
).then(
441442
(result) {
442443
return TimeOfDay.fromDateTime(result ?? currentValue);
@@ -447,9 +448,7 @@ class _FormBuilderDateTimePickerState
447448
context,
448449
showTitleActions: true,
449450
currentTime: currentValue,
450-
locale: enumValueFromString(
451-
(widget.locale ?? Localizations.localeOf(context))?.languageCode,
452-
LocaleType.values),
451+
locale: _localeType(),
453452
).then(
454453
(result) {
455454
return TimeOfDay.fromDateTime(result ?? currentValue);
@@ -487,7 +486,7 @@ class _FormBuilderDateTimePickerState
487486
}
488487

489488
@override
490-
void patchValue(dynamic val) {
489+
void patchValue(DateTime val) {
491490
super.patchValue(val);
492491
_textFieldController.text = val == null ? '' : dateFormat.format(val);
493492
}

lib/src/utils.dart

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)