Skip to content

Commit 9e561a4

Browse files
committed
Use locale for default DateTimePicker format
1 parent 5f464da commit 9e561a4

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

example/lib/main.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
22
import 'package:flutter/gestures.dart';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_form_builder/flutter_form_builder.dart';
5+
import 'package:flutter_localizations/flutter_localizations.dart';
56
import 'package:intl/intl.dart';
67

78
import 'data.dart';
@@ -21,6 +22,13 @@ class MyApp extends StatelessWidget {
2122
border: OutlineInputBorder(gapPadding: 10),
2223
),
2324
),
25+
localizationsDelegates: [
26+
GlobalMaterialLocalizations.delegate,
27+
GlobalWidgetsLocalizations.delegate,
28+
GlobalCupertinoLocalizations.delegate,
29+
],
30+
// locale: Locale('ru'),
31+
supportedLocales: [Locale('en', 'GB'), Locale('en', 'US'), Locale('es', ''), Locale('ru'), ],
2432
home: MyHomePage(),
2533
);
2634
}
@@ -173,6 +181,7 @@ class MyHomePageState extends State<MyHomePage> {
173181
labelText: 'Appointment Time',
174182
),
175183
validator: (val) => null,
184+
// locale: Locale('ru'),
176185
initialTime: TimeOfDay(hour: 8, minute: 0),
177186
// initialValue: DateTime.now(),
178187
// readonly: true,

example/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ environment:
99
dependencies:
1010
flutter:
1111
sdk: flutter
12+
flutter_localizations:
13+
sdk: flutter
1214

1315
flutter_form_builder:
1416
path: ../

lib/src/fields/form_builder_date_time_picker.dart

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,22 @@ class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
235235
FocusNode _focusNode;
236236
TextEditingController _textFieldController;
237237
DateTime stateCurrentValue;
238-
DateFormat _dateFormat;
239238

240-
final _dateTimeFormats = {
241-
InputType.both: DateFormat("EEEE, MMMM d, yyyy 'at' h:mma"),
242-
InputType.date: DateFormat('yyyy-MM-dd'),
243-
InputType.time: DateFormat('HH:mm'),
244-
};
239+
DateFormat get dateFormat =>
240+
widget.format ?? _dateTimeFormats[widget.inputType];
241+
Map _dateTimeFormats;
242+
243+
/*@override
244+
void didChangeDependencies() {
245+
var myLocale = widget.locale ?? Localizations.localeOf(context);
246+
print(myLocale);
247+
_dateTimeFormats = {
248+
InputType.both: DateFormat.yMMMd(myLocale.toString()).add_Hms(),
249+
InputType.date: DateFormat.yMd(myLocale.toString()),
250+
InputType.time: DateFormat.Hm(myLocale.toString()),
251+
};
252+
super.didChangeDependencies();
253+
}*/
245254

246255
@override
247256
void initState() {
@@ -255,9 +264,14 @@ class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
255264
stateCurrentValue = _initialValue;
256265
_focusNode = widget.focusNode ?? FocusNode();
257266
_textFieldController = widget.controller ?? TextEditingController();
258-
_dateFormat = widget.format ?? _dateTimeFormats[widget.inputType];
267+
// var appLocale = Localizations.localeOf(context);
268+
_dateTimeFormats = {
269+
InputType.both: DateFormat.yMd(widget.locale?.toString()).add_Hms(),
270+
InputType.date: DateFormat.yMd(widget.locale?.toString()),
271+
InputType.time: DateFormat.Hm(widget.locale?.toString()),
272+
};
259273
_textFieldController.text =
260-
_initialValue == null ? '' : _dateFormat.format(_initialValue);
274+
_initialValue == null ? '' : dateFormat.format(_initialValue);
261275
_focusNode.addListener(_handleFocus);
262276
}
263277

@@ -291,7 +305,7 @@ class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
291305
child: DateTimeField(
292306
key: _fieldKey,
293307
initialValue: _initialValue,
294-
format: _dateFormat,
308+
format: dateFormat,
295309
onSaved: (val) {
296310
var value = _fieldKey.currentState.value;
297311
var transformed;

0 commit comments

Comments
 (0)