Skip to content

Commit 3113e83

Browse files
fix: FormBuilderCupertinoDateTimePicker layout
1 parent 04c0cfb commit 3113e83

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

example/lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ class _MyHomePageState extends State<MyHomePage> {
104104
const SizedBox(height: 16),
105105
FormBuilderCupertinoDateTimePicker(
106106
name: 'date',
107+
prefix: const Icon(CupertinoIcons.calendar),
107108
autovalidateMode: AutovalidateMode.onUserInteraction,
108109
validator: (value) => value != null ? null : 'Required date',
110+
initialValue: DateTime.now(),
109111
),
110112
],
111113
),

lib/src/fields/form_builder_cupertino_datetime_picker.dart

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'package:intl/intl.dart';
88

99
import 'package:flutter_form_builder/flutter_form_builder.dart';
1010

11+
/**************** START Imported from flutter/material.dart ****************/
12+
1113
/// Initial display of a calendar date picker.
1214
///
1315
/// Either a grid of available years or a monthly calendar.
@@ -91,10 +93,9 @@ enum DatePickerEntryMode {
9193
/// There is no user interface to switch to another mode.
9294
inputOnly,
9395
}
96+
/**************** END Imported from flutter/material.dart ****************/
9497

95-
enum InputType { date, time, both }
96-
97-
/// Field for `Date`, `Time` and `DateTime` input
98+
/// A [CupertinoDateTimePicker] that integrates with [FormBuilder].
9899
class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
99100
/// The date/time picker dialogs to show.
100101
final InputType inputType;
@@ -154,6 +155,43 @@ class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
154155
final TextAlign textAlign;
155156
final TextAlignVertical? textAlignVertical;
156157

158+
/// Defines whether the field input expands to fill the entire width
159+
/// of the row field.
160+
///
161+
/// By default `false`
162+
final bool shouldExpandedField;
163+
164+
/// A widget that is displayed at the start of the row.
165+
///
166+
/// The [prefix] parameter is displayed at the start of the row. Standard iOS
167+
/// guidelines encourage passing a [Text] widget to [prefix] to detail the
168+
/// nature of the row's [child] widget. If null, the [child] widget will take
169+
/// up all horizontal space in the row.
170+
final Widget? prefix;
171+
172+
/// Content padding for the row.
173+
///
174+
/// Defaults to the standard iOS padding for form rows. If no edge insets are
175+
/// intended, explicitly pass [EdgeInsets.zero] to [contentPadding].
176+
final EdgeInsetsGeometry? contentPadding;
177+
178+
/// A widget that is displayed underneath the [prefix] and [child] widgets.
179+
///
180+
/// The [helper] appears in primary label coloring, and is meant to inform the
181+
/// user about interaction with the child widget. The row becomes taller in
182+
/// order to display the [helper] widget underneath [prefix] and [child]. If
183+
/// null, the row is shorter.
184+
final Widget? helper;
185+
186+
/// A builder widget that is displayed underneath the [prefix] and [child] widgets.
187+
///
188+
/// The [error] widget is primarily used to inform users of input errors. When
189+
/// a [Text] is given to [error], it will be shown in
190+
/// [CupertinoColors.destructiveRed] coloring and medium-weighted font. The
191+
/// row becomes taller in order to display the [helper] widget underneath
192+
/// [prefix] and [child]. If null, the row is shorter.
193+
final Widget? Function(String error)? errorBuilder;
194+
157195
/// Preset the widget's value.
158196
final bool autofocus;
159197
final bool obscureText;
@@ -188,17 +226,9 @@ class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
188226
final double cursorWidth;
189227
final TextCapitalization textCapitalization;
190228

191-
final String? cancelText;
192-
final String? confirmText;
193-
final String? errorFormatText;
194-
final String? errorInvalidText;
195-
final String? fieldHintText;
196-
final String? fieldLabelText;
197-
final String? helpText;
198229
final DatePickerEntryMode initialEntryMode;
199230
final RouteSettings? routeSettings;
200231

201-
final TimePickerEntryMode timePickerInitialEntryMode;
202232
final StrutStyle? strutStyle;
203233
final bool Function(DateTime day)? selectableDayPredicate;
204234
final Offset? anchorPoint;
@@ -226,6 +256,11 @@ class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
226256
this.initialTime = const TimeOfDay(hour: 12, minute: 0),
227257
this.keyboardType,
228258
this.textAlign = TextAlign.start,
259+
this.shouldExpandedField = false,
260+
this.prefix,
261+
this.contentPadding,
262+
this.helper,
263+
this.errorBuilder,
229264
this.autofocus = false,
230265
this.obscureText = false,
231266
this.autocorrect = true,
@@ -236,7 +271,6 @@ class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
236271
this.textCapitalization = TextCapitalization.none,
237272
this.useRootNavigator = true,
238273
this.initialEntryMode = DatePickerEntryMode.calendar,
239-
this.timePickerInitialEntryMode = TimePickerEntryMode.dial,
240274
this.format,
241275
this.initialDate,
242276
this.firstDate,
@@ -258,13 +292,6 @@ class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
258292
this.cursorRadius = const Radius.circular(2.0),
259293
this.cursorColor,
260294
this.keyboardAppearance,
261-
this.cancelText,
262-
this.confirmText,
263-
this.errorFormatText,
264-
this.errorInvalidText,
265-
this.fieldHintText,
266-
this.fieldLabelText,
267-
this.helpText,
268295
this.routeSettings,
269296
this.strutStyle,
270297
this.selectableDayPredicate,
@@ -275,7 +302,7 @@ class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
275302
builder: (FormFieldState<DateTime?> field) {
276303
final state = field as _FormBuilderCupertinoDateTimePickerState;
277304

278-
return FocusTraversalGroup(
305+
final fieldWidget = FocusTraversalGroup(
279306
policy: ReadingOrderTraversalPolicy(),
280307
child: CupertinoTextField(
281308
onTap: () => state.showPicker(),
@@ -284,7 +311,6 @@ class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
284311
textAlignVertical: textAlignVertical,
285312
maxLength: maxLength,
286313
autofocus: autofocus,
287-
// FIXME: decoration: state.decoration,
288314
readOnly: true,
289315
enabled: state.enabled,
290316
autocorrect: autocorrect,
@@ -311,6 +337,20 @@ class FormBuilderCupertinoDateTimePicker extends FormBuilderField<DateTime> {
311337
maxLengthEnforcement: maxLengthEnforcement,
312338
),
313339
);
340+
341+
return CupertinoFormRow(
342+
error: state.hasError
343+
? errorBuilder != null
344+
? errorBuilder(state.errorText ?? '')
345+
: Text(state.errorText ?? '')
346+
: null,
347+
helper: helper,
348+
padding: contentPadding,
349+
prefix: prefix,
350+
child: shouldExpandedField
351+
? SizedBox(width: double.infinity, child: fieldWidget)
352+
: fieldWidget,
353+
);
314354
},
315355
);
316356

0 commit comments

Comments
 (0)