From 07cc3f7a617e435b0718c7b4ad1206816d780e53 Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Thu, 21 Sep 2023 07:54:19 +0200 Subject: [PATCH] Form fields onChange callback should be called on reset (#134295) ## Description This PR fixes form fields in order to call the `onChange` callback when the form is reset. This change is based on the work done in https://github.com/flutter/flutter/pull/123108. I considered adding the `onChange` callback to the `FormField` superclass but it would break existing code because two of the three subclasses defines the `onChange` callback with `ValueChanged?` type and the third one defines it with `ValueChanged?`. ## Related Issue Fixes https://github.com/flutter/flutter/issues/123009. ## Tests Adds 3 tests. --- .../src/cupertino/text_form_field_row.dart | 19 +++--- .../flutter/lib/src/material/dropdown.dart | 11 +++- .../lib/src/material/text_form_field.dart | 17 +++-- .../cupertino/text_form_field_row_test.dart | 39 ++++++++++++ .../material/dropdown_form_field_test.dart | 48 ++++++++++++++ .../test/material/text_form_field_test.dart | 62 +++++++++++++++++++ 6 files changed, 177 insertions(+), 19 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_form_field_row.dart b/packages/flutter/lib/src/cupertino/text_form_field_row.dart index 674c62237e..5f9c951ebd 100644 --- a/packages/flutter/lib/src/cupertino/text_form_field_row.dart +++ b/packages/flutter/lib/src/cupertino/text_form_field_row.dart @@ -133,7 +133,7 @@ class CupertinoTextFormFieldRow extends FormField { int? minLines, bool expands = false, int? maxLength, - ValueChanged? onChanged, + this.onChanged, GestureTapCallback? onTap, VoidCallback? onEditingComplete, ValueChanged? onFieldSubmitted, @@ -179,9 +179,7 @@ class CupertinoTextFormFieldRow extends FormField { void onChangedHandler(String value) { field.didChange(value); - if (onChanged != null) { - onChanged(value); - } + onChanged?.call(value); } return CupertinoFormRow( @@ -260,6 +258,9 @@ class CupertinoTextFormFieldRow extends FormField { /// initialize its [TextEditingController.text] with [initialValue]. final TextEditingController? controller; + /// {@macro flutter.material.TextFormField.onChanged} + final ValueChanged? onChanged; + static Widget _defaultContextMenuBuilder(BuildContext context, EditableTextState editableTextState) { return CupertinoAdaptiveTextSelectionToolbar.editableText( editableTextState: editableTextState, @@ -328,13 +329,11 @@ class _CupertinoTextFormFieldRowState extends FormFieldState { @override void reset() { + // Set the controller value before calling super.reset() to let + // _handleControllerChanged suppress the change. + _effectiveController!.text = widget.initialValue!; super.reset(); - - if (widget.initialValue != null) { - setState(() { - _effectiveController!.text = widget.initialValue!; - }); - } + _cupertinoTextFormFieldRow.onChanged?.call(_effectiveController!.text); } void _handleControllerChanged() { diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index a72eba1b90..7194881092 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -1736,13 +1736,12 @@ class DropdownButtonFormField extends FormField { } class _DropdownButtonFormFieldState extends FormFieldState { + DropdownButtonFormField get _dropdownButtonFormField => widget as DropdownButtonFormField; @override void didChange(T? value) { super.didChange(value); - final DropdownButtonFormField dropdownButtonFormField = widget as DropdownButtonFormField; - assert(dropdownButtonFormField.onChanged != null); - dropdownButtonFormField.onChanged!(value); + _dropdownButtonFormField.onChanged!(value); } @override @@ -1752,4 +1751,10 @@ class _DropdownButtonFormFieldState extends FormFieldState { setValue(widget.initialValue); } } + + @override + void reset() { + super.reset(); + _dropdownButtonFormField.onChanged!(value); + } } diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index b388d932aa..a283b96915 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -131,7 +131,7 @@ class TextFormField extends FormField { int? minLines, bool expands = false, int? maxLength, - ValueChanged? onChanged, + this.onChanged, GestureTapCallback? onTap, TapRegionCallback? onTapOutside, VoidCallback? onEditingComplete, @@ -193,9 +193,7 @@ class TextFormField extends FormField { .applyDefaults(Theme.of(field.context).inputDecorationTheme); void onChangedHandler(String value) { field.didChange(value); - if (onChanged != null) { - onChanged(value); - } + onChanged?.call(value); } return UnmanagedRestorationScope( bucket: field.bucket, @@ -272,6 +270,12 @@ class TextFormField extends FormField { /// initialize its [TextEditingController.text] with [initialValue]. final TextEditingController? controller; + /// {@template flutter.material.TextFormField.onChanged} + /// Called when the user initiates a change to the TextField's + /// value: when they have inserted or deleted text or reset the form. + /// {@endtemplate} + final ValueChanged? onChanged; + static Widget _defaultContextMenuBuilder(BuildContext context, EditableTextState editableTextState) { return AdaptiveTextSelectionToolbar.editableText( editableTextState: editableTextState, @@ -365,10 +369,11 @@ class _TextFormFieldState extends FormFieldState { @override void reset() { - // setState will be called in the superclass, so even though state is being - // manipulated, no setState call is needed here. + // Set the controller value before calling super.reset() to let + // _handleControllerChanged suppress the change. _effectiveController.text = widget.initialValue ?? ''; super.reset(); + _textFormField.onChanged?.call(_effectiveController.text); } void _handleControllerChanged() { diff --git a/packages/flutter/test/cupertino/text_form_field_row_test.dart b/packages/flutter/test/cupertino/text_form_field_row_test.dart index ff48a0e2a8..9ddff783b6 100644 --- a/packages/flutter/test/cupertino/text_form_field_row_test.dart +++ b/packages/flutter/test/cupertino/text_form_field_row_test.dart @@ -490,4 +490,43 @@ void main() { final CupertinoTextField rtlTextFieldWidget = tester.widget(rtlTextFieldFinder); expect(rtlTextFieldWidget.textDirection, TextDirection.rtl); }); + + testWidgets('CupertinoTextFormFieldRow onChanged is called when the form is reset', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/123009. + final GlobalKey> stateKey = GlobalKey>(); + final GlobalKey formKey = GlobalKey(); + String value = 'initialValue'; + + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: Form( + key: formKey, + child: CupertinoTextFormFieldRow( + key: stateKey, + initialValue: value, + onChanged: (String newValue) { + value = newValue; + }, + ), + ), + ), + ), + ); + + // Initial value is 'initialValue'. + expect(stateKey.currentState!.value, 'initialValue'); + expect(value, 'initialValue'); + + // Change value to 'changedValue'. + await tester.enterText(find.byType(CupertinoTextField), 'changedValue'); + expect(stateKey.currentState!.value,'changedValue'); + expect(value, 'changedValue'); + + // Should be back to 'initialValue' when the form is reset. + formKey.currentState!.reset(); + await tester.pump(); + expect(stateKey.currentState!.value,'initialValue'); + expect(value, 'initialValue'); + }); } diff --git a/packages/flutter/test/material/dropdown_form_field_test.dart b/packages/flutter/test/material/dropdown_form_field_test.dart index 094c8cb30e..6d8faf8b24 100644 --- a/packages/flutter/test/material/dropdown_form_field_test.dart +++ b/packages/flutter/test/material/dropdown_form_field_test.dart @@ -1231,4 +1231,52 @@ void main() { inkWell = tester.widget(find.byType(InkWell)); expect(inkWell.borderRadius, errorBorderRadius); }); + + testWidgets('DropdownButtonFormField onChanged is called when the form is reset', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/123009. + final GlobalKey> stateKey = GlobalKey>(); + final GlobalKey formKey = GlobalKey(); + String? value; + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Form( + key: formKey, + child: DropdownButtonFormField( + key: stateKey, + value: 'One', + items: ['One', 'Two', 'Free', 'Four'] + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(), + onChanged: (String? newValue) { + value = newValue; + }, + ), + ), + ), + ), + ); + + // Initial value is 'One'. + expect(value, isNull); + expect(stateKey.currentState!.value, equals('One')); + + // Select 'Two'. + await tester.tap(find.text('One')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Two').last); + await tester.pumpAndSettle(); + expect(value, equals('Two')); + expect(stateKey.currentState!.value, equals('Two')); + + // Should be back to 'One' when the form is reset. + formKey.currentState!.reset(); + expect(value, equals('One')); + expect(stateKey.currentState!.value, equals('One')); + }); } diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index 9d52c9c0de..b3bdc07fc7 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -816,6 +816,31 @@ void main() { expect(find.text('initialValue'), findsOneWidget); }); + testWidgetsWithLeakTracking('reset resets the text fields value to the controller initial value', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(text: 'initialValue'); + addTearDown(controller.dispose); + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: TextFormField( + controller: controller, + ), + ), + ), + ), + ); + + await tester.enterText(find.byType(TextFormField), 'changedValue'); + + final FormFieldState state = tester.state>(find.byType(TextFormField)); + state.reset(); + + expect(find.text('changedValue'), findsNothing); + expect(find.text('initialValue'), findsOneWidget); + }); + // Regression test for https://github.com/flutter/flutter/issues/34847. testWidgetsWithLeakTracking("didChange resets the text field's value to empty when passed null", (WidgetTester tester) async { await tester.pumpWidget( @@ -1478,4 +1503,41 @@ void main() { await tester.pump(); expect(textField.cursorColor, errorColor); }); + + testWidgetsWithLeakTracking('TextFormField onChanged is called when the form is reset', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/123009. + final GlobalKey> stateKey = GlobalKey>(); + final GlobalKey formKey = GlobalKey(); + String value = 'initialValue'; + + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: Form( + key: formKey, + child: TextFormField( + key: stateKey, + initialValue: value, + onChanged: (String newValue) { + value = newValue; + }, + ), + ), + ), + )); + + // Initial value is 'initialValue'. + expect(stateKey.currentState!.value, 'initialValue'); + expect(value, 'initialValue'); + + // Change value to 'changedValue'. + await tester.enterText(find.byType(TextField), 'changedValue'); + expect(stateKey.currentState!.value,'changedValue'); + expect(value, 'changedValue'); + + // Should be back to 'initialValue' when the form is reset. + formKey.currentState!.reset(); + await tester.pump(); + expect(stateKey.currentState!.value,'initialValue'); + expect(value, 'initialValue'); + }); }