From b9af6551b2a97675b66273230f21920de44b915a Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 19 Jun 2020 13:53:05 -0700 Subject: [PATCH] Label unnecessarily ellided (#59807) --- .../lib/src/material/input_decorator.dart | 8 ++- .../test/material/input_decorator_test.dart | 60 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 893d81e722..af84401c38 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -986,9 +986,15 @@ class _RenderDecoration extends RenderBox { + contentPadding.right)); // Increase the available width for the label when it is scaled down. final double invertedLabelScale = lerpDouble(1.00, 1 / _kFinalLabelScale, decoration.floatingLabelProgress); + final double labelWidth = math.max(0.0, constraints.maxWidth - ( + _boxSize(icon).width + + contentPadding.left + + _boxSize(prefixIcon).width + + _boxSize(suffixIcon).width + + contentPadding.right)); boxToBaseline[label] = _layoutLineBox( label, - boxConstraints.copyWith(maxWidth: inputWidth * invertedLabelScale), + boxConstraints.copyWith(maxWidth: labelWidth * invertedLabelScale), ); boxToBaseline[hint] = _layoutLineBox( hint, diff --git a/packages/flutter/test/material/input_decorator_test.dart b/packages/flutter/test/material/input_decorator_test.dart index 7070ee4c72..cccbef6b7d 100644 --- a/packages/flutter/test/material/input_decorator_test.dart +++ b/packages/flutter/test/material/input_decorator_test.dart @@ -4158,4 +4158,64 @@ void main() { expect(tester.getTopLeft(find.text(hintText)).dy, topPosition); }); + + testWidgets("InputDecorator label width isn't affected by prefix or suffix", (WidgetTester tester) async { + const String labelText = 'My Label'; + const String prefixText = 'The five boxing wizards jump quickly.'; + const String suffixText = 'Suffix'; + + Widget getLabeledInputDecorator(bool showFix) { + return MaterialApp( + home: Material( + child: Builder( + builder: (BuildContext context) { + return Theme( + data: Theme.of(context), + child: Align( + alignment: Alignment.topLeft, + child: TextField( + decoration: InputDecoration( + icon: const Icon(Icons.assistant), + prefixText: showFix ? prefixText : null, + suffixText: showFix ? suffixText : null, + suffixIcon: const Icon(Icons.threesixty), + labelText: labelText, + ), + ), + ), + ); + }, + ), + ), + ); + } + + // Build with no prefix or suffix. + await tester.pumpWidget(getLabeledInputDecorator(false)); + + // Get the width of the label when there is no prefix/suffix. + expect(find.text(prefixText), findsNothing); + expect(find.text(suffixText), findsNothing); + final double labelWidth = tester.getSize(find.text(labelText)).width; + + // Build with a prefix and suffix. + await tester.pumpWidget(getLabeledInputDecorator(true)); + + // The prefix and suffix exist but aren't visible. They have not affected + // the width of the label. + expect(find.text(prefixText), findsOneWidget); + expect(getOpacity(tester, prefixText), 0.0); + expect(find.text(suffixText), findsOneWidget); + expect(getOpacity(tester, suffixText), 0.0); + expect(tester.getSize(find.text(labelText)).width, labelWidth); + + // Tap to focus. + await tester.tap(find.byType(TextField)); + await tester.pumpAndSettle(); + + // The prefix and suffix are visible, and the label is floating and still + // hasn't had its width affected. + expect(tester.getSize(find.text(labelText)).width, labelWidth); + expect(getOpacity(tester, prefixText), 1.0); + }); }