From e972d5a3f6469d2391e2851531e04ba154584d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9=20Dumazy?= Date: Thu, 10 Aug 2023 20:53:56 +0200 Subject: [PATCH] Add hasInteractedByUser getter in FormField (#131539) Adds a getter to access the value of the private `RestorableBool _hasInteractedByUser`. *List which issues are fixed by this PR. You must list at least one issue.* Fixes #131538 --- packages/flutter/lib/src/widgets/form.dart | 6 ++ packages/flutter/test/widgets/form_test.dart | 86 ++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/packages/flutter/lib/src/widgets/form.dart b/packages/flutter/lib/src/widgets/form.dart index 76e8521ab9..0860532a78 100644 --- a/packages/flutter/lib/src/widgets/form.dart +++ b/packages/flutter/lib/src/widgets/form.dart @@ -423,6 +423,12 @@ class FormFieldState extends State> with RestorationMixin { /// True if this field has any validation errors. bool get hasError => _errorText.value != null; + /// Returns true if the user has modified the value of this field. + /// + /// This only updates to true once [didChange] has been called and resets to + /// false when [reset] is called. + bool get hasInteractedByUser => _hasInteractedByUser.value; + /// True if the current value is valid. /// /// This will not set [errorText] or [hasError] and it will not update diff --git a/packages/flutter/test/widgets/form_test.dart b/packages/flutter/test/widgets/form_test.dart index 98d10c636a..6fa4bbd42a 100644 --- a/packages/flutter/test/widgets/form_test.dart +++ b/packages/flutter/test/widgets/form_test.dart @@ -854,4 +854,90 @@ void main() { expect(fieldValue, '123456'); expect(formKey.currentState!.validate(), isFalse); }); + + testWidgets('hasInteractedByUser returns false when the input has not changed', (WidgetTester tester) async { + final GlobalKey> fieldKey = GlobalKey>(); + + final Widget widget = MaterialApp( + home: MediaQuery( + data: const MediaQueryData(), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: Material( + child: TextFormField( + key: fieldKey, + ), + ), + ), + ), + ), + ); + + await tester.pumpWidget(widget); + + expect(fieldKey.currentState!.hasInteractedByUser, isFalse); + }); + + testWidgets('hasInteractedByUser returns true after the input has changed', (WidgetTester tester) async { + final GlobalKey> fieldKey = GlobalKey>(); + + final Widget widget = MaterialApp( + home: MediaQuery( + data: const MediaQueryData(), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: Material( + child: TextFormField( + key: fieldKey, + ), + ), + ), + ), + ), + ); + + await tester.pumpWidget(widget); + + // initially, the field has not been interacted with + expect(fieldKey.currentState!.hasInteractedByUser, isFalse); + + // after entering text, the field has been interacted with + await tester.enterText(find.byType(TextFormField), 'foo'); + expect(fieldKey.currentState!.hasInteractedByUser, isTrue); + }); + + testWidgets('hasInteractedByUser returns false after the field is reset', (WidgetTester tester) async { + final GlobalKey> fieldKey = GlobalKey>(); + + final Widget widget = MaterialApp( + home: MediaQuery( + data: const MediaQueryData(), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: Material( + child: TextFormField( + key: fieldKey, + ), + ), + ), + ), + ), + ); + + await tester.pumpWidget(widget); + + // initially, the field has not been interacted with + expect(fieldKey.currentState!.hasInteractedByUser, isFalse); + + // after entering text, the field has been interacted with + await tester.enterText(find.byType(TextFormField), 'foo'); + expect(fieldKey.currentState!.hasInteractedByUser, isTrue); + + // after resetting the field, it has not been interacted with again + fieldKey.currentState!.reset(); + expect(fieldKey.currentState!.hasInteractedByUser, isFalse); + }); }