From 0220c3585c12e3f9d3bb778d90b5476b0316a8e8 Mon Sep 17 00:00:00 2001 From: YeungKC Date: Wed, 16 Sep 2020 23:13:05 +0800 Subject: [PATCH] Fix FormFieldState value not in sync with the onChanged value from TextFormField. (#65695) --- .../lib/src/material/text_form_field.dart | 2 +- .../test/material/text_form_field_test.dart | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index fabfdca9f0..44440d7b89 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -232,10 +232,10 @@ class TextFormField extends FormField { final InputDecoration effectiveDecoration = (decoration ?? const InputDecoration()) .applyDefaults(Theme.of(field.context).inputDecorationTheme); void onChangedHandler(String value) { + field.didChange(value); if (onChanged != null) { onChanged(value); } - field.didChange(value); } return TextField( controller: state._effectiveController, diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index 2f67336cd5..c347726845 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -431,6 +431,33 @@ void main() { expect(find.text('changedValue'), findsOneWidget); }); + testWidgets('onChanged callbacks value and FormFieldState.value are sync', (WidgetTester tester) async { + bool _called = false; + + FormFieldState state; + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: TextFormField( + onChanged: (String value) { + _called = true; + expect(value, state.value); + }, + ), + ), + ), + ), + ); + + state = tester.state>(find.byType(TextFormField)); + + await tester.enterText(find.byType(TextField), 'Soup'); + + expect(_called, true); + }); + testWidgets('autofillHints is passed to super', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp(