Skip to content

Commit 5f464da

Browse files
committed
Fixed bug in DateRangePicker where onChanged fires before change. Closes #434
The field is also cleared if change is cancelled.
1 parent bc9b8cf commit 5f464da

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

lib/src/fields/form_builder_date_range_picker.dart

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,9 @@ class FormBuilderDateRangePickerState
166166
((_formState?.initialValue?.containsKey(widget.attribute) ?? false)
167167
? _formState.initialValue[widget.attribute]
168168
: []);
169-
_controller = TextEditingController(
170-
text: _valueToText(),
171-
);
172-
_effectiveController.addListener(() {
173-
widget.onChanged?.call(_effectiveController.text);
174-
});
169+
_controller = TextEditingController(text: _valueToText());
175170
_focusNode = FocusNode();
176-
widget.focusNode?.addListener(_handleFocus);
177-
_focusNode?.addListener(_handleFocus);
171+
_effectiveFocusNode?.addListener(_handleFocus);
178172
super.initState();
179173
}
180174

@@ -261,9 +255,12 @@ class FormBuilderDateRangePickerState
261255
textDirection: widget.textDirection,
262256
selectableDayPredicate: widget.selectableDayPredicate,
263257
);
264-
_fieldKey.currentState.didChange(picked);
265-
_setCurrentValue(picked);
266-
_effectiveController.text = _valueToText();
258+
if (picked != null) {
259+
_fieldKey.currentState.didChange(picked);
260+
widget.onChanged?.call(picked);
261+
_setCurrentValue(picked);
262+
_effectiveController.text = _valueToText();
263+
}
267264
}
268265
}
269266

@@ -291,8 +288,9 @@ class FormBuilderDateRangePickerState
291288

292289
@override
293290
void dispose() {
291+
_focusNode?.dispose();
294292
_formState?.unregisterFieldKey(widget.attribute);
295-
_controller.dispose();
293+
_controller?.dispose();
296294
super.dispose();
297295
}
298296
}

0 commit comments

Comments
 (0)