Skip to content

Commit de0aab1

Browse files
Issue 88543: TextField labelText doesn't get hintTextStyle when labelTextStyle is non-null Fixed (flutter#89386)
labelText now gets hintTextStyle when no labelStyle, per the docs
1 parent f9ff834 commit de0aab1

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,12 +2231,14 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
22312231
isHovering: isHovering,
22322232
);
22332233

2234+
final TextStyle? inlineLabelStyle = decoration!.label != null ? decoration!.labelStyle : decoration!.hintStyle;
2235+
22342236
// Temporary opt-in fix for https://github.com/flutter/flutter/issues/54028
22352237
// Setting TextStyle.height to 1 ensures that the label's height will equal
22362238
// its font size.
2237-
final TextStyle inlineLabelStyle = themeData.fixTextFieldOutlineLabel
2238-
? inlineStyle.merge(decoration!.labelStyle).copyWith(height: 1)
2239-
: inlineStyle.merge(decoration!.labelStyle);
2239+
final TextStyle effectiveInlineLabelStyle = themeData.fixTextFieldOutlineLabel
2240+
? inlineStyle.merge(inlineLabelStyle).copyWith(height: 1)
2241+
: inlineStyle.merge(inlineLabelStyle);
22402242
final Widget? label = decoration!.labelText == null && decoration!.label == null ? null : _Shaker(
22412243
animation: _shakingLabelController.view,
22422244
child: AnimatedOpacity(
@@ -2248,7 +2250,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
22482250
curve: _kTransitionCurve,
22492251
style: widget._labelShouldWithdraw
22502252
? _getFloatingLabelStyle(themeData)
2251-
: inlineLabelStyle,
2253+
: effectiveInlineLabelStyle,
22522254
child: decoration!.label ?? Text(
22532255
decoration!.labelText!,
22542256
overflow: TextOverflow.ellipsis,
@@ -2371,7 +2373,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
23712373
contentPadding = decorationContentPadding ?? EdgeInsets.zero;
23722374
} else if (!border.isOutline) {
23732375
// 4.0: the vertical gap between the inline elements and the floating label.
2374-
floatingLabelHeight = (4.0 + 0.75 * inlineLabelStyle.fontSize!) * MediaQuery.textScaleFactorOf(context);
2376+
floatingLabelHeight = (4.0 + 0.75 * effectiveInlineLabelStyle.fontSize!) * MediaQuery.textScaleFactorOf(context);
23752377
if (decoration!.filled == true) { // filled == null same as filled == false
23762378
contentPadding = decorationContentPadding ?? (decorationIsDense
23772379
? const EdgeInsets.fromLTRB(12.0, 8.0, 12.0, 8.0)

packages/flutter/test/material/input_decorator_test.dart

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3422,7 +3422,7 @@ void main() {
34223422
expect(tester.widget<Text>(find.text('suffix')).style!.color, suffixStyle.color);
34233423
expect(tester.widget<Text>(find.text('helper')).style!.color, helperStyle.color);
34243424
expect(tester.widget<Text>(find.text('counter')).style!.color, counterStyle.color);
3425-
expect(getLabelStyle(tester).color, labelStyle.color);
3425+
expect(getLabelStyle(tester).color, hintStyle.color);
34263426
});
34273427

34283428
testWidgets('InputDecorationTheme style overrides (focused)', (WidgetTester tester) async {
@@ -5244,4 +5244,66 @@ void main() {
52445244
// Verify that the styles were passed along
52455245
expect(getLabelStyle(tester).color, labelStyle.color);
52465246
});
5247+
5248+
testWidgets('hintStyle is used for labelText when labelText is on top of the input field and labelStyle is used for labelText when labelText moves above widget', (WidgetTester tester) async {
5249+
const TextStyle style16 = TextStyle(fontFamily: 'Ahem', fontSize: 16.0);
5250+
final TextStyle labelStyle = style16.merge(const TextStyle(color: Colors.purple));
5251+
final TextStyle hintStyle = style16.merge(const TextStyle(color: Colors.red));
5252+
5253+
await tester.pumpWidget(
5254+
buildInputDecorator(
5255+
isEmpty: true,
5256+
isFocused: false, // Label appears inline, on top of the input field.
5257+
decoration: InputDecoration(
5258+
labelText: 'label',
5259+
labelStyle: labelStyle,
5260+
hintStyle: hintStyle,
5261+
),
5262+
),
5263+
);
5264+
5265+
// Overall height for this InputDecorator is 56dps:
5266+
// 12 - top padding
5267+
// 12 - floating label (ahem font size 16dps * 0.75 = 12)
5268+
// 4 - floating label / input text gap
5269+
// 16 - input text (ahem font size 16dps)
5270+
// 12 - bottom padding
5271+
expect(tester.getSize(find.byType(InputDecorator)), const Size(800.0, 56.0));
5272+
expect(tester.getTopLeft(find.text('label')).dy, 20.0);
5273+
expect(tester.getBottomLeft(find.text('label')).dy, 36.0);
5274+
expect(getBorderBottom(tester), 56.0);
5275+
expect(getBorderWeight(tester), 1.0);
5276+
5277+
// Verify that the hintStyle was used
5278+
expect(getLabelStyle(tester).color, hintStyle.color);
5279+
5280+
await tester.pumpWidget(
5281+
buildInputDecorator(
5282+
isEmpty: true,
5283+
isFocused: true, // Label moves above (i.e., vertically adjacent to) the input field
5284+
decoration: InputDecoration(
5285+
labelText: 'label',
5286+
labelStyle: labelStyle,
5287+
hintStyle: hintStyle,
5288+
),
5289+
),
5290+
);
5291+
5292+
await tester.pumpAndSettle();
5293+
5294+
// Overall height for this InputDecorator is 56dps:
5295+
// 12 - top padding
5296+
// 12 - floating label (ahem font size 16dps * 0.75 = 12)
5297+
// 4 - floating label / input text gap
5298+
// 16 - input text (ahem font size 16dps)
5299+
// 12 - bottom padding
5300+
expect(tester.getSize(find.byType(InputDecorator)), const Size(800.0, 56.0));
5301+
expect(tester.getTopLeft(find.text('label')).dy, 12.0);
5302+
expect(tester.getBottomLeft(find.text('label')).dy, 24.0);
5303+
expect(getBorderBottom(tester), 56.0);
5304+
expect(getBorderWeight(tester), 2.0);
5305+
5306+
// Verify that the labelStyle was used
5307+
expect(getLabelStyle(tester).color, labelStyle.color);
5308+
});
52475309
}

0 commit comments

Comments
 (0)