Skip to content

Commit b9af655

Browse files
authored
Label unnecessarily ellided (flutter#59807)
1 parent 65b1956 commit b9af655

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

packages/flutter/lib/src/material/input_decorator.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,9 +986,15 @@ class _RenderDecoration extends RenderBox {
986986
+ contentPadding.right));
987987
// Increase the available width for the label when it is scaled down.
988988
final double invertedLabelScale = lerpDouble(1.00, 1 / _kFinalLabelScale, decoration.floatingLabelProgress);
989+
final double labelWidth = math.max(0.0, constraints.maxWidth - (
990+
_boxSize(icon).width
991+
+ contentPadding.left
992+
+ _boxSize(prefixIcon).width
993+
+ _boxSize(suffixIcon).width
994+
+ contentPadding.right));
989995
boxToBaseline[label] = _layoutLineBox(
990996
label,
991-
boxConstraints.copyWith(maxWidth: inputWidth * invertedLabelScale),
997+
boxConstraints.copyWith(maxWidth: labelWidth * invertedLabelScale),
992998
);
993999
boxToBaseline[hint] = _layoutLineBox(
9941000
hint,

packages/flutter/test/material/input_decorator_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,4 +4158,64 @@ void main() {
41584158

41594159
expect(tester.getTopLeft(find.text(hintText)).dy, topPosition);
41604160
});
4161+
4162+
testWidgets("InputDecorator label width isn't affected by prefix or suffix", (WidgetTester tester) async {
4163+
const String labelText = 'My Label';
4164+
const String prefixText = 'The five boxing wizards jump quickly.';
4165+
const String suffixText = 'Suffix';
4166+
4167+
Widget getLabeledInputDecorator(bool showFix) {
4168+
return MaterialApp(
4169+
home: Material(
4170+
child: Builder(
4171+
builder: (BuildContext context) {
4172+
return Theme(
4173+
data: Theme.of(context),
4174+
child: Align(
4175+
alignment: Alignment.topLeft,
4176+
child: TextField(
4177+
decoration: InputDecoration(
4178+
icon: const Icon(Icons.assistant),
4179+
prefixText: showFix ? prefixText : null,
4180+
suffixText: showFix ? suffixText : null,
4181+
suffixIcon: const Icon(Icons.threesixty),
4182+
labelText: labelText,
4183+
),
4184+
),
4185+
),
4186+
);
4187+
},
4188+
),
4189+
),
4190+
);
4191+
}
4192+
4193+
// Build with no prefix or suffix.
4194+
await tester.pumpWidget(getLabeledInputDecorator(false));
4195+
4196+
// Get the width of the label when there is no prefix/suffix.
4197+
expect(find.text(prefixText), findsNothing);
4198+
expect(find.text(suffixText), findsNothing);
4199+
final double labelWidth = tester.getSize(find.text(labelText)).width;
4200+
4201+
// Build with a prefix and suffix.
4202+
await tester.pumpWidget(getLabeledInputDecorator(true));
4203+
4204+
// The prefix and suffix exist but aren't visible. They have not affected
4205+
// the width of the label.
4206+
expect(find.text(prefixText), findsOneWidget);
4207+
expect(getOpacity(tester, prefixText), 0.0);
4208+
expect(find.text(suffixText), findsOneWidget);
4209+
expect(getOpacity(tester, suffixText), 0.0);
4210+
expect(tester.getSize(find.text(labelText)).width, labelWidth);
4211+
4212+
// Tap to focus.
4213+
await tester.tap(find.byType(TextField));
4214+
await tester.pumpAndSettle();
4215+
4216+
// The prefix and suffix are visible, and the label is floating and still
4217+
// hasn't had its width affected.
4218+
expect(tester.getSize(find.text(labelText)).width, labelWidth);
4219+
expect(getOpacity(tester, prefixText), 1.0);
4220+
});
41614221
}

0 commit comments

Comments
 (0)