From 43ce3fd98833b534555ef79ac17b1481ab5a5689 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Tue, 28 Apr 2020 10:49:02 -0700 Subject: [PATCH] TextField enabled fix (#55775) --- .../flutter/lib/src/material/text_field.dart | 2 +- .../lib/src/material/text_form_field.dart | 6 +-- .../test/material/text_field_test.dart | 46 ++++++++++++++++++ .../test/material/text_form_field_test.dart | 48 +++++++++++++++++++ 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index 24056025eab..7fef72632a3 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -802,7 +802,7 @@ class _TextFieldState extends State implements TextSelectionGestureDe final InputDecoration effectiveDecoration = (widget.decoration ?? const InputDecoration()) .applyDefaults(themeData.inputDecorationTheme) .copyWith( - enabled: widget.enabled, + enabled: _isEnabled, hintMaxLines: widget.decoration?.hintMaxLines ?? widget.maxLines, ); diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index 8d235754f78..72955b49639 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -165,7 +165,7 @@ class TextFormField extends FormField { FormFieldSetter onSaved, FormFieldValidator validator, List inputFormatters, - bool enabled = true, + bool enabled, double cursorWidth = 2.0, Radius cursorRadius, Color cursorColor, @@ -205,7 +205,7 @@ class TextFormField extends FormField { onSaved: onSaved, validator: validator, autovalidate: autovalidate, - enabled: enabled, + enabled: enabled ?? decoration?.enabled ?? true, builder: (FormFieldState field) { final _TextFormFieldState state = field as _TextFormFieldState; final InputDecoration effectiveDecoration = (decoration ?? const InputDecoration()) @@ -248,7 +248,7 @@ class TextFormField extends FormField { onEditingComplete: onEditingComplete, onSubmitted: onFieldSubmitted, inputFormatters: inputFormatters, - enabled: enabled, + enabled: enabled ?? decoration?.enabled ?? true, cursorWidth: cursorWidth, cursorRadius: cursorRadius, cursorColor: cursorColor, diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index bcaa972f43c..2425222f7fc 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -3597,6 +3597,52 @@ void main() { semantics.dispose(); }); + testWidgets('Disabled text field hides helper and counter', (WidgetTester tester) async { + const String helperText = 'helper text'; + const String counterText = 'counter text'; + const String errorText = 'error text'; + Widget buildFrame(bool enabled, bool hasError) { + return MaterialApp( + home: Material( + child: Center( + child: TextField( + decoration: InputDecoration( + labelText: 'label text', + helperText: helperText, + counterText: counterText, + errorText: hasError ? errorText : null, + enabled: enabled, + ), + ), + ), + ), + ); + } + + await tester.pumpWidget(buildFrame(true, false)); + Text helperWidget = tester.widget(find.text(helperText)); + Text counterWidget = tester.widget(find.text(counterText)); + expect(helperWidget.style.color, isNot(equals(Colors.transparent))); + expect(counterWidget.style.color, isNot(equals(Colors.transparent))); + await tester.pumpWidget(buildFrame(true, true)); + counterWidget = tester.widget(find.text(counterText)); + Text errorWidget = tester.widget(find.text(errorText)); + expect(helperWidget.style.color, isNot(equals(Colors.transparent))); + expect(errorWidget.style.color, isNot(equals(Colors.transparent))); + + // When enabled is false, the helper/error and counter are not visible. + await tester.pumpWidget(buildFrame(false, false)); + helperWidget = tester.widget(find.text(helperText)); + counterWidget = tester.widget(find.text(counterText)); + expect(helperWidget.style.color, equals(Colors.transparent)); + expect(counterWidget.style.color, equals(Colors.transparent)); + await tester.pumpWidget(buildFrame(false, true)); + errorWidget = tester.widget(find.text(errorText)); + counterWidget = tester.widget(find.text(counterText)); + expect(counterWidget.style.color, equals(Colors.transparent)); + expect(errorWidget.style.color, equals(Colors.transparent)); + }); + testWidgets('currentValueLength/maxValueLength are in the tree', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester); final TextEditingController controller = TextEditingController(); diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index 02a84b6184b..bc7c9dbbde4 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -240,6 +240,54 @@ void main() { expect(_validateCalled, 2); }); + + testWidgets('Disabled field hides helper and counter', (WidgetTester tester) async { + const String helperText = 'helper text'; + const String counterText = 'counter text'; + const String errorText = 'error text'; + Widget buildFrame(bool enabled, bool hasError) { + return MaterialApp( + home: Material( + child: Center( + child: TextFormField( + decoration: InputDecoration( + labelText: 'label text', + helperText: helperText, + counterText: counterText, + errorText: hasError ? errorText : null, + enabled: enabled, + ), + ), + ), + ), + ); + } + + // When enabled is true, the helper/error and counter are visible. + await tester.pumpWidget(buildFrame(true, false)); + Text helperWidget = tester.widget(find.text(helperText)); + Text counterWidget = tester.widget(find.text(counterText)); + expect(helperWidget.style.color, isNot(equals(Colors.transparent))); + expect(counterWidget.style.color, isNot(equals(Colors.transparent))); + await tester.pumpWidget(buildFrame(true, true)); + counterWidget = tester.widget(find.text(counterText)); + Text errorWidget = tester.widget(find.text(errorText)); + expect(helperWidget.style.color, isNot(equals(Colors.transparent))); + expect(errorWidget.style.color, isNot(equals(Colors.transparent))); + + // When enabled is false, the helper/error and counter are not visible. + await tester.pumpWidget(buildFrame(false, false)); + helperWidget = tester.widget(find.text(helperText)); + counterWidget = tester.widget(find.text(counterText)); + expect(helperWidget.style.color, equals(Colors.transparent)); + expect(counterWidget.style.color, equals(Colors.transparent)); + await tester.pumpWidget(buildFrame(false, true)); + errorWidget = tester.widget(find.text(errorText)); + counterWidget = tester.widget(find.text(counterText)); + expect(counterWidget.style.color, equals(Colors.transparent)); + expect(errorWidget.style.color, equals(Colors.transparent)); + }); + testWidgets('passing a buildCounter shows returned widget', (WidgetTester tester) async { await tester.pumpWidget(MaterialApp( home: Material(