Skip to content

Commit c3f3126

Browse files
committed
Added alwaysUse24HourFormat option to DateTimePicker. Closes #297
1 parent 1caa758 commit c3f3126

File tree

1 file changed

+79
-58
lines changed

1 file changed

+79
-58
lines changed

lib/src/fields/form_builder_date_time_picker.dart

Lines changed: 79 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class FormBuilderDateTimePicker extends StatefulWidget {
137137

138138
final double cursorWidth;
139139
final TextCapitalization textCapitalization;
140+
final bool alwaysUse24HourFormat;
140141

141142
FormBuilderDateTimePicker({
142143
Key key,
@@ -194,6 +195,7 @@ class FormBuilderDateTimePicker extends StatefulWidget {
194195
this.textCapitalization = TextCapitalization.none,
195196
this.strutStyle,
196197
this.useRootNavigator = true,
198+
this.alwaysUse24HourFormat = false,
197199
}) : super(key: key);
198200

199201
final StrutStyle strutStyle;
@@ -257,62 +259,67 @@ class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
257259
Widget build(BuildContext context) {
258260
_readOnly = (_formState?.readOnly == true) ? true : widget.readOnly;
259261

260-
return DateTimeField(
261-
key: _fieldKey,
262-
initialValue: _initialValue,
263-
format: _dateFormat,
264-
onSaved: (val) {
265-
var value = _fieldKey.currentState.value;
266-
var transformed;
267-
if (widget.valueTransformer != null) {
268-
transformed = widget.valueTransformer(val);
269-
_formState?.setAttributeValue(widget.attribute, transformed);
270-
} else {
271-
_formState?.setAttributeValue(widget.attribute, value);
272-
}
273-
if (widget.onSaved != null) {
274-
widget.onSaved(transformed ?? value);
275-
}
276-
},
277-
validator: (val) =>
278-
FormBuilderValidators.validateValidators(val, widget.validators),
279-
onShowPicker: _onShowPicker,
280-
onChanged: (val) {
281-
if (widget.onChanged != null) widget.onChanged(val);
282-
},
283-
autovalidate: widget.autovalidate,
284-
resetIcon: widget.resetIcon,
285-
textDirection: widget.textDirection,
286-
textAlign: widget.textAlign,
287-
maxLength: widget.maxLength,
288-
autofocus: widget.autofocus,
289-
decoration: widget.decoration,
290-
readOnly: true,
291-
enabled: !_readOnly,
292-
autocorrect: widget.autocorrect,
293-
controller: _textFieldController,
294-
focusNode: _focusNode,
295-
inputFormatters: widget.inputFormatters,
296-
keyboardType: widget.keyboardType,
297-
maxLengthEnforced: widget.maxLengthEnforced,
298-
maxLines: widget.maxLines,
299-
obscureText: widget.obscureText,
300-
showCursor: widget.showCursor,
301-
minLines: widget.minLines,
302-
expands: widget.expands,
303-
style: widget.style,
304-
onEditingComplete: widget.onEditingComplete,
305-
buildCounter: widget.buildCounter,
306-
cursorColor: widget.cursorColor,
307-
cursorRadius: widget.cursorRadius,
308-
cursorWidth: widget.cursorWidth,
309-
enableInteractiveSelection: widget.enableInteractiveSelection,
310-
keyboardAppearance: widget.keyboardAppearance,
311-
onFieldSubmitted: widget.onFieldSubmitted,
312-
scrollPadding: widget.scrollPadding,
313-
strutStyle: widget.strutStyle,
314-
textCapitalization: widget.textCapitalization,
315-
textInputAction: widget.textInputAction,
262+
return MediaQuery(
263+
data: MediaQuery.of(context).copyWith(
264+
alwaysUse24HourFormat: true,
265+
),
266+
child: DateTimeField(
267+
key: _fieldKey,
268+
initialValue: _initialValue,
269+
format: _dateFormat,
270+
onSaved: (val) {
271+
var value = _fieldKey.currentState.value;
272+
var transformed;
273+
if (widget.valueTransformer != null) {
274+
transformed = widget.valueTransformer(val);
275+
_formState?.setAttributeValue(widget.attribute, transformed);
276+
} else {
277+
_formState?.setAttributeValue(widget.attribute, value);
278+
}
279+
if (widget.onSaved != null) {
280+
widget.onSaved(transformed ?? value);
281+
}
282+
},
283+
validator: (val) =>
284+
FormBuilderValidators.validateValidators(val, widget.validators),
285+
onShowPicker: _onShowPicker,
286+
onChanged: (val) {
287+
if (widget.onChanged != null) widget.onChanged(val);
288+
},
289+
autovalidate: widget.autovalidate,
290+
resetIcon: widget.resetIcon,
291+
textDirection: widget.textDirection,
292+
textAlign: widget.textAlign,
293+
maxLength: widget.maxLength,
294+
autofocus: widget.autofocus,
295+
decoration: widget.decoration,
296+
readOnly: true,
297+
enabled: !_readOnly,
298+
autocorrect: widget.autocorrect,
299+
controller: _textFieldController,
300+
focusNode: _focusNode,
301+
inputFormatters: widget.inputFormatters,
302+
keyboardType: widget.keyboardType,
303+
maxLengthEnforced: widget.maxLengthEnforced,
304+
maxLines: widget.maxLines,
305+
obscureText: widget.obscureText,
306+
showCursor: widget.showCursor,
307+
minLines: widget.minLines,
308+
expands: widget.expands,
309+
style: widget.style,
310+
onEditingComplete: widget.onEditingComplete,
311+
buildCounter: widget.buildCounter,
312+
cursorColor: widget.cursorColor,
313+
cursorRadius: widget.cursorRadius,
314+
cursorWidth: widget.cursorWidth,
315+
enableInteractiveSelection: widget.enableInteractiveSelection,
316+
keyboardAppearance: widget.keyboardAppearance,
317+
onFieldSubmitted: widget.onFieldSubmitted,
318+
scrollPadding: widget.scrollPadding,
319+
strutStyle: widget.strutStyle,
320+
textCapitalization: widget.textCapitalization,
321+
textInputAction: widget.textInputAction,
322+
),
316323
);
317324
}
318325

@@ -361,7 +368,14 @@ class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
361368
locale: widget.locale,
362369
textDirection: widget.textDirection,
363370
useRootNavigator: widget.useRootNavigator,
364-
builder: widget.builder,
371+
builder: widget.builder ??
372+
(BuildContext context, Widget child) {
373+
return MediaQuery(
374+
data: MediaQuery.of(context).copyWith(
375+
alwaysUse24HourFormat: widget.alwaysUse24HourFormat),
376+
child: child,
377+
);
378+
},
365379
);
366380
}
367381
}
@@ -376,7 +390,14 @@ class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
376390
initialTime: currentValue != null
377391
? TimeOfDay.fromDateTime(currentValue)
378392
: widget.initialTime ?? TimeOfDay.fromDateTime(DateTime.now()),
379-
builder: widget.builder,
393+
builder: widget.builder ??
394+
(BuildContext context, Widget child) {
395+
return MediaQuery(
396+
data: MediaQuery.of(context).copyWith(
397+
alwaysUse24HourFormat: widget.alwaysUse24HourFormat),
398+
child: child,
399+
);
400+
},
380401
useRootNavigator: widget.useRootNavigator,
381402
).then(
382403
(result) {

0 commit comments

Comments
 (0)