@@ -134,40 +134,44 @@ class FormBuilderDateRangePicker
134
134
builder: (FormFieldState <DateTimeRange ?> field) {
135
135
final state = field as _FormBuilderDateRangePickerState ;
136
136
137
- return TextField (
138
- enabled: state.enabled,
139
- style: style,
140
- focusNode: state.effectiveFocusNode,
141
- decoration: state.decoration,
142
- // initialValue: "${_initialValue ?? ''}",
143
- maxLines: maxLines,
144
- keyboardType: keyboardType,
145
- obscureText: obscureText,
146
- onEditingComplete: onEditingComplete,
147
- controller: state._effectiveController,
148
- autocorrect: autocorrect,
149
- autofocus: autofocus,
150
- buildCounter: buildCounter,
151
- mouseCursor: mouseCursor,
152
- cursorColor: cursorColor,
153
- cursorRadius: cursorRadius,
154
- cursorWidth: cursorWidth,
155
- enableInteractiveSelection: enableInteractiveSelection,
156
- maxLength: maxLength,
157
- inputFormatters: inputFormatters,
158
- keyboardAppearance: keyboardAppearance,
159
- maxLengthEnforcement: maxLengthEnforcement,
160
- scrollPadding: scrollPadding,
161
- textAlign: textAlign,
162
- textCapitalization: textCapitalization,
163
- textDirection: textDirection,
164
- textInputAction: textInputAction,
165
- textAlignVertical: textAlignVertical,
166
- strutStyle: strutStyle,
167
- readOnly: true ,
168
- expands: expands,
169
- minLines: minLines,
170
- showCursor: showCursor,
137
+ return FocusTraversalGroup (
138
+ policy: ReadingOrderTraversalPolicy (),
139
+ child: TextField (
140
+ onTap: () => state.showPicker (),
141
+ enabled: state.enabled,
142
+ style: style,
143
+ focusNode: state.effectiveFocusNode,
144
+ decoration: state.decoration,
145
+ // initialValue: "${_initialValue ?? ''}",
146
+ maxLines: maxLines,
147
+ keyboardType: keyboardType,
148
+ obscureText: obscureText,
149
+ onEditingComplete: onEditingComplete,
150
+ controller: state._effectiveController,
151
+ autocorrect: autocorrect,
152
+ autofocus: autofocus,
153
+ buildCounter: buildCounter,
154
+ mouseCursor: mouseCursor,
155
+ cursorColor: cursorColor,
156
+ cursorRadius: cursorRadius,
157
+ cursorWidth: cursorWidth,
158
+ enableInteractiveSelection: enableInteractiveSelection,
159
+ maxLength: maxLength,
160
+ inputFormatters: inputFormatters,
161
+ keyboardAppearance: keyboardAppearance,
162
+ maxLengthEnforcement: maxLengthEnforcement,
163
+ scrollPadding: scrollPadding,
164
+ textAlign: textAlign,
165
+ textCapitalization: textCapitalization,
166
+ textDirection: textDirection,
167
+ textInputAction: textInputAction,
168
+ textAlignVertical: textAlignVertical,
169
+ strutStyle: strutStyle,
170
+ readOnly: true ,
171
+ expands: expands,
172
+ minLines: minLines,
173
+ showCursor: showCursor,
174
+ ),
171
175
);
172
176
},
173
177
);
@@ -195,55 +199,57 @@ class _FormBuilderDateRangePickerState extends FormBuilderFieldDecorationState<
195
199
super .initState ();
196
200
_effectiveController =
197
201
widget.controller ?? TextEditingController (text: _valueToText ());
198
- effectiveFocusNode.addListener (_handleFocus);
202
+
203
+ effectiveFocusNode.onKeyEvent = (node, event) {
204
+ if (enabled &&
205
+ event is KeyDownEvent &&
206
+ event.logicalKey == LogicalKeyboardKey .space &&
207
+ node.hasFocus) {
208
+ showPicker ();
209
+ return KeyEventResult .handled;
210
+ }
211
+ return KeyEventResult .ignored;
212
+ };
199
213
}
200
214
201
215
@override
202
216
void dispose () {
203
- effectiveFocusNode.removeListener (_handleFocus);
204
217
// Dispose the _effectiveController when initState created it
205
218
if (null == widget.controller) {
206
219
_effectiveController.dispose ();
207
220
}
208
221
super .dispose ();
209
222
}
210
223
211
- Future <void > _handleFocus () async {
212
- if (effectiveFocusNode.hasFocus && enabled) {
213
- effectiveFocusNode.unfocus ();
214
- /*final initialFirstDate = value?.isEmpty ?? true
215
- ? (widget.initialFirstDate ?? DateTime.now())
216
- : value[0];
217
- final initialLastDate = value?.isEmpty ?? true
218
- ? (widget.initialLastDate ?? initialFirstDate)
219
- : (value.length < 2 ? initialFirstDate : value[1]);*/
220
- final picked = await showDateRangePicker (
221
- context: context,
222
- firstDate: widget.firstDate,
223
- lastDate: widget.lastDate,
224
- locale: widget.locale,
225
- textDirection: widget.textDirection,
226
- cancelText: widget.cancelText,
227
- confirmText: widget.confirmText,
228
- currentDate: widget.currentDate,
229
- errorFormatText: widget.errorFormatText,
230
- builder: widget.pickerBuilder,
231
- errorInvalidRangeText: widget.errorInvalidRangeText,
232
- errorInvalidText: widget.errorInvalidText,
233
- fieldEndHintText: widget.fieldEndHintText,
234
- fieldEndLabelText: widget.fieldEndLabelText,
235
- fieldStartHintText: widget.fieldStartHintText,
236
- fieldStartLabelText: widget.fieldStartLabelText,
237
- helpText: widget.helpText,
238
- initialDateRange: value,
239
- initialEntryMode: widget.initialEntryMode,
240
- routeSettings: widget.routeSettings,
241
- saveText: widget.saveText,
242
- useRootNavigator: widget.useRootNavigator,
243
- );
244
- if (picked != null ) {
245
- didChange (picked);
246
- }
224
+ Future <void > showPicker () async {
225
+ effectiveFocusNode.requestFocus ();
226
+
227
+ final picked = await showDateRangePicker (
228
+ context: context,
229
+ firstDate: widget.firstDate,
230
+ lastDate: widget.lastDate,
231
+ locale: widget.locale,
232
+ textDirection: widget.textDirection,
233
+ cancelText: widget.cancelText,
234
+ confirmText: widget.confirmText,
235
+ currentDate: widget.currentDate,
236
+ errorFormatText: widget.errorFormatText,
237
+ builder: widget.pickerBuilder,
238
+ errorInvalidRangeText: widget.errorInvalidRangeText,
239
+ errorInvalidText: widget.errorInvalidText,
240
+ fieldEndHintText: widget.fieldEndHintText,
241
+ fieldEndLabelText: widget.fieldEndLabelText,
242
+ fieldStartHintText: widget.fieldStartHintText,
243
+ fieldStartLabelText: widget.fieldStartLabelText,
244
+ helpText: widget.helpText,
245
+ initialDateRange: value,
246
+ initialEntryMode: widget.initialEntryMode,
247
+ routeSettings: widget.routeSettings,
248
+ saveText: widget.saveText,
249
+ useRootNavigator: widget.useRootNavigator,
250
+ );
251
+ if (picked != null ) {
252
+ didChange (picked);
247
253
}
248
254
}
249
255
@@ -277,14 +283,15 @@ class _FormBuilderDateRangePickerState extends FormBuilderFieldDecorationState<
277
283
@override
278
284
InputDecoration get decoration => widget.allowClear
279
285
? super .decoration.copyWith (
280
- suffix: IconButton (
286
+ suffix: IconButton (
281
287
padding: EdgeInsets .zero,
282
288
constraints: const BoxConstraints (maxWidth: 24 , maxHeight: 24 ),
283
289
onPressed: () {
284
290
focus ();
285
291
didChange (null );
286
- effectiveFocusNode.unfocus ();
287
292
},
288
- icon: widget.clearIcon ?? const Icon (Icons .clear)))
293
+ icon: widget.clearIcon ?? const Icon (Icons .clear),
294
+ ),
295
+ )
289
296
: super .decoration;
290
297
}
0 commit comments