From 6758b55c8d03386920015904cbc11fd806b7645e Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Sun, 18 Mar 2018 21:54:03 -0700 Subject: [PATCH] Fix weird bug when switching text from errorText to helperText (#15390) --- .../lib/src/material/input_decorator.dart | 16 +++++++++++----- .../flutter/lib/src/material/slider_theme.dart | 5 +++++ .../flutter/lib/src/material/text_field.dart | 2 ++ packages/flutter/lib/src/widgets/form.dart | 4 ++++ .../material/text_field_helper_text_test.dart | 17 +++++++++++++++++ 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 packages/flutter/test/material/text_field_helper_text_test.dart diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 1db08eab88..43c5e3c665 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -277,16 +277,19 @@ class _HelperErrorState extends State<_HelperError> with SingleTickerProviderSta void didUpdateWidget(_HelperError old) { super.didUpdateWidget(old); - final String errorText = widget.errorText; - final String helperText = widget.helperText; + final String newErrorText = widget.errorText; + final String newHelperText = widget.helperText; final String oldErrorText = old.errorText; final String oldHelperText = old.helperText; - if ((errorText ?? helperText) != (oldErrorText ?? oldHelperText)) { - if (errorText != null) { + final bool errorTextStateChanged = (newErrorText != null) != (oldErrorText != null); + final bool helperTextStateChanged = newErrorText == null && (newHelperText != null) != (oldHelperText != null); + + if (errorTextStateChanged || helperTextStateChanged) { + if (newErrorText != null) { _error = _buildError(); _controller.forward(); - } else if (helperText != null) { + } else if (newHelperText != null) { _helper = _buildHelper(); _controller.reverse(); } else { @@ -1818,6 +1821,9 @@ class InputDecoration { /// /// If non-null, the border's color animates to red and the [helperText] is /// not shown. + /// + /// In a [TextFormField], this is overridden by the value returned from + /// [TextFormField.validator], if that is not null. final String errorText; /// The style to use for the [errorText]. diff --git a/packages/flutter/lib/src/material/slider_theme.dart b/packages/flutter/lib/src/material/slider_theme.dart index 9983db21ab..6d5a7eed8b 100644 --- a/packages/flutter/lib/src/material/slider_theme.dart +++ b/packages/flutter/lib/src/material/slider_theme.dart @@ -337,6 +337,8 @@ class SliderThemeData extends Diagnosticable { /// when the thumb is being touched. final ShowValueIndicator showValueIndicator; + /// Creates a copy of this object but with the given fields replaced with the + /// new values. SliderThemeData copyWith({ Color activeRailColor, Color inactiveRailColor, @@ -537,7 +539,9 @@ abstract class SliderComponentShape { /// * [SliderThemeData] where an instance of this class is set to inform the /// slider of the shape of the its thumb. class RoundSliderThumbShape extends SliderComponentShape { + /// Create a slider thumb that draws a circle. const RoundSliderThumbShape(); + static const double _thumbRadius = 6.0; static const double _disabledThumbRadius = 4.0; @@ -585,6 +589,7 @@ class RoundSliderThumbShape extends SliderComponentShape { /// * [SliderThemeData] where an instance of this class is set to inform the /// slider of the shape of the its value indicator. class PaddleSliderValueIndicatorShape extends SliderComponentShape { + /// Create a slider value indicator in the shape of an upside-down pear. const PaddleSliderValueIndicatorShape(); // These constants define the shape of the default value indicator. diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index e9e012d5cf..08a9b11d81 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -213,6 +213,8 @@ class TextField extends StatefulWidget { /// characters may be entered, but the error counter and divider will /// switch to the [decoration.errorStyle] when the limit is exceeded. /// + /// ## Limitations + /// /// The TextField does not currently count Unicode grapheme clusters (i.e. /// characters visible to the user), it counts Unicode scalar values, which /// leaves out a number of useful possible characters (like many emoji and diff --git a/packages/flutter/lib/src/widgets/form.dart b/packages/flutter/lib/src/widgets/form.dart index 8272fa55c6..602db6ffad 100644 --- a/packages/flutter/lib/src/widgets/form.dart +++ b/packages/flutter/lib/src/widgets/form.dart @@ -218,6 +218,10 @@ class FormField extends StatefulWidget { /// An optional method that validates an input. Returns an error string to /// display if the input is invalid, or null otherwise. + /// + /// The returned value is exposed by the [FormFieldState.errorText] property. + /// The [TextFormField] uses this to override the [InputDecoration.errorText] + /// value. final FormFieldValidator validator; /// Function that returns the widget representing this form field. It is diff --git a/packages/flutter/test/material/text_field_helper_text_test.dart b/packages/flutter/test/material/text_field_helper_text_test.dart new file mode 100644 index 0000000000..44514eb591 --- /dev/null +++ b/packages/flutter/test/material/text_field_helper_text_test.dart @@ -0,0 +1,17 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('TextField works correctly when changing helperText', (WidgetTester tester) async { + await tester.pumpWidget(new MaterialApp(home: const TextField(decoration: const InputDecoration(helperText: 'Awesome')))); + expect(find.text('Awesome'), findsNWidgets(1)); + await tester.pump(const Duration(milliseconds: 100)); + expect(find.text('Awesome'), findsNWidgets(1)); + await tester.pumpWidget(new MaterialApp(home: const TextField(decoration: const InputDecoration(errorText: 'Awesome')))); + expect(find.text('Awesome'), findsNWidgets(2)); + }); +} \ No newline at end of file