From a12c22b296d25bfb06c70b222fbf533e04f1e718 Mon Sep 17 00:00:00 2001 From: xubaolin Date: Sat, 5 Dec 2020 03:28:03 +0800 Subject: [PATCH] Set [InputDecoration.floatingLabelBehavior] default to null (#70683) --- .../lib/src/material/input_decorator.dart | 10 ++-- .../test/material/input_decorator_test.dart | 2 +- .../test/material/text_field_test.dart | 56 ++++++++++++++++++- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 025fa6aa17..a992100d4a 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -2518,7 +2518,7 @@ class InputDecoration { 'This feature was deprecated after v1.13.2.' ) this.hasFloatingPlaceholder = true, - this.floatingLabelBehavior = FloatingLabelBehavior.auto, + this.floatingLabelBehavior, this.isCollapsed = false, this.isDense, this.contentPadding, @@ -2564,7 +2564,7 @@ class InputDecoration { 'This feature was deprecated after v1.13.2.' ) this.hasFloatingPlaceholder = true, - this.floatingLabelBehavior = FloatingLabelBehavior.auto, + this.floatingLabelBehavior, this.hintStyle, this.filled = false, this.fillColor, @@ -2752,9 +2752,9 @@ class InputDecoration { /// /// When [FloatingLabelBehavior.never] the label will always appear in an empty /// field in place of the content. - /// - /// Defaults to [FloatingLabelBehavior.auto]. /// {@endtemplate} + /// + /// If null, [InputDecorationTheme.floatingLabelBehavior] will be used. final FloatingLabelBehavior? floatingLabelBehavior; /// Whether the [InputDecorator.child] is part of a dense form (i.e., uses less vertical @@ -3700,6 +3700,8 @@ class InputDecorationTheme with Diagnosticable { final bool hasFloatingPlaceholder; /// {@macro flutter.material.inputDecoration.floatingLabelBehavior} + /// + /// Defaults to [FloatingLabelBehavior.auto]. final FloatingLabelBehavior floatingLabelBehavior; /// Whether the input decorator's child is part of a dense form (i.e., uses diff --git a/packages/flutter/test/material/input_decorator_test.dart b/packages/flutter/test/material/input_decorator_test.dart index d628b2feb2..461dae1233 100644 --- a/packages/flutter/test/material/input_decorator_test.dart +++ b/packages/flutter/test/material/input_decorator_test.dart @@ -3052,7 +3052,7 @@ void main() { ); expect( child.toString(), - "InputDecorator-[<'key'>](decoration: InputDecoration(floatingLabelBehavior: FloatingLabelBehavior.auto), baseStyle: TextStyle(), isFocused: false, isEmpty: false)", + "InputDecorator-[<'key'>](decoration: InputDecoration(), baseStyle: TextStyle(), isFocused: false, isEmpty: false)", ); }); diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index b84ccce50b..39771c8053 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -7524,7 +7524,7 @@ void main() { expect(description, [ 'enabled: false', - 'decoration: InputDecoration(labelText: "foo", floatingLabelBehavior: FloatingLabelBehavior.auto)', + 'decoration: InputDecoration(labelText: "foo")', 'style: TextStyle(inherit: true, color: Color(0xff00ff00))', 'autofocus: true', 'autocorrect: false', @@ -8691,4 +8691,58 @@ void main() { expect(inputWidth, wideWidth); expect(cursorRight, inputWidth - kCaretGap); }); + + // Regressing test for https://github.com/flutter/flutter/issues/70625 + testWidgets('TextFields can inherit [FloatingLabelBehaviour] from InputDecorationTheme.', (WidgetTester tester) async { + final FocusNode focusNode = FocusNode(); + Widget textFieldBuilder({FloatingLabelBehavior behavior = FloatingLabelBehavior.auto, }) { + return MaterialApp( + theme: ThemeData( + inputDecorationTheme: InputDecorationTheme( + floatingLabelBehavior: behavior, + ), + ), + home: Scaffold( + body: TextField( + focusNode: focusNode, + decoration: const InputDecoration( + labelText: 'Label', + ), + ), + ), + ); + } + + await tester.pumpWidget(textFieldBuilder(behavior: FloatingLabelBehavior.auto)); + // The label will be positioned within the content when unfocused. + expect(tester.getTopLeft(find.text('Label')).dy, 20.0); + + focusNode.requestFocus(); + await tester.pumpAndSettle(); // label animation. + // The label will float above the content when focused. + expect(tester.getTopLeft(find.text('Label')).dy, 12.0); + + focusNode.unfocus(); + await tester.pumpAndSettle(); // label animation. + + await tester.pumpWidget(textFieldBuilder(behavior: FloatingLabelBehavior.never)); + await tester.pumpAndSettle(); // theme animation. + // The label will be positioned within the content. + expect(tester.getTopLeft(find.text('Label')).dy, 20.0); + + focusNode.requestFocus(); + await tester.pumpAndSettle(); // label animation. + // The label will always be positioned within the content. + expect(tester.getTopLeft(find.text('Label')).dy, 20.0); + + await tester.pumpWidget(textFieldBuilder(behavior: FloatingLabelBehavior.always)); + await tester.pumpAndSettle(); // theme animation. + // The label will always float above the content. + expect(tester.getTopLeft(find.text('Label')).dy, 12.0); + + focusNode.unfocus(); + await tester.pumpAndSettle(); // label animation. + // The label will always float above the content. + expect(tester.getTopLeft(find.text('Label')).dy, 12.0); + }); }