diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 333fed6660..301335e45a 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -2231,12 +2231,14 @@ class _InputDecoratorState extends State with TickerProviderStat isHovering: isHovering, ); + final TextStyle? inlineLabelStyle = decoration!.label != null ? decoration!.labelStyle : decoration!.hintStyle; + // Temporary opt-in fix for https://github.com/flutter/flutter/issues/54028 // Setting TextStyle.height to 1 ensures that the label's height will equal // its font size. - final TextStyle inlineLabelStyle = themeData.fixTextFieldOutlineLabel - ? inlineStyle.merge(decoration!.labelStyle).copyWith(height: 1) - : inlineStyle.merge(decoration!.labelStyle); + final TextStyle effectiveInlineLabelStyle = themeData.fixTextFieldOutlineLabel + ? inlineStyle.merge(inlineLabelStyle).copyWith(height: 1) + : inlineStyle.merge(inlineLabelStyle); final Widget? label = decoration!.labelText == null && decoration!.label == null ? null : _Shaker( animation: _shakingLabelController.view, child: AnimatedOpacity( @@ -2248,7 +2250,7 @@ class _InputDecoratorState extends State with TickerProviderStat curve: _kTransitionCurve, style: widget._labelShouldWithdraw ? _getFloatingLabelStyle(themeData) - : inlineLabelStyle, + : effectiveInlineLabelStyle, child: decoration!.label ?? Text( decoration!.labelText!, overflow: TextOverflow.ellipsis, @@ -2371,7 +2373,7 @@ class _InputDecoratorState extends State with TickerProviderStat contentPadding = decorationContentPadding ?? EdgeInsets.zero; } else if (!border.isOutline) { // 4.0: the vertical gap between the inline elements and the floating label. - floatingLabelHeight = (4.0 + 0.75 * inlineLabelStyle.fontSize!) * MediaQuery.textScaleFactorOf(context); + floatingLabelHeight = (4.0 + 0.75 * effectiveInlineLabelStyle.fontSize!) * MediaQuery.textScaleFactorOf(context); if (decoration!.filled == true) { // filled == null same as filled == false contentPadding = decorationContentPadding ?? (decorationIsDense ? const EdgeInsets.fromLTRB(12.0, 8.0, 12.0, 8.0) diff --git a/packages/flutter/test/material/input_decorator_test.dart b/packages/flutter/test/material/input_decorator_test.dart index 080eb5c6f0..d25326b7d4 100644 --- a/packages/flutter/test/material/input_decorator_test.dart +++ b/packages/flutter/test/material/input_decorator_test.dart @@ -3422,7 +3422,7 @@ void main() { expect(tester.widget(find.text('suffix')).style!.color, suffixStyle.color); expect(tester.widget(find.text('helper')).style!.color, helperStyle.color); expect(tester.widget(find.text('counter')).style!.color, counterStyle.color); - expect(getLabelStyle(tester).color, labelStyle.color); + expect(getLabelStyle(tester).color, hintStyle.color); }); testWidgets('InputDecorationTheme style overrides (focused)', (WidgetTester tester) async { @@ -5244,4 +5244,66 @@ void main() { // Verify that the styles were passed along expect(getLabelStyle(tester).color, labelStyle.color); }); + + 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 { + const TextStyle style16 = TextStyle(fontFamily: 'Ahem', fontSize: 16.0); + final TextStyle labelStyle = style16.merge(const TextStyle(color: Colors.purple)); + final TextStyle hintStyle = style16.merge(const TextStyle(color: Colors.red)); + + await tester.pumpWidget( + buildInputDecorator( + isEmpty: true, + isFocused: false, // Label appears inline, on top of the input field. + decoration: InputDecoration( + labelText: 'label', + labelStyle: labelStyle, + hintStyle: hintStyle, + ), + ), + ); + + // Overall height for this InputDecorator is 56dps: + // 12 - top padding + // 12 - floating label (ahem font size 16dps * 0.75 = 12) + // 4 - floating label / input text gap + // 16 - input text (ahem font size 16dps) + // 12 - bottom padding + expect(tester.getSize(find.byType(InputDecorator)), const Size(800.0, 56.0)); + expect(tester.getTopLeft(find.text('label')).dy, 20.0); + expect(tester.getBottomLeft(find.text('label')).dy, 36.0); + expect(getBorderBottom(tester), 56.0); + expect(getBorderWeight(tester), 1.0); + + // Verify that the hintStyle was used + expect(getLabelStyle(tester).color, hintStyle.color); + + await tester.pumpWidget( + buildInputDecorator( + isEmpty: true, + isFocused: true, // Label moves above (i.e., vertically adjacent to) the input field + decoration: InputDecoration( + labelText: 'label', + labelStyle: labelStyle, + hintStyle: hintStyle, + ), + ), + ); + + await tester.pumpAndSettle(); + + // Overall height for this InputDecorator is 56dps: + // 12 - top padding + // 12 - floating label (ahem font size 16dps * 0.75 = 12) + // 4 - floating label / input text gap + // 16 - input text (ahem font size 16dps) + // 12 - bottom padding + expect(tester.getSize(find.byType(InputDecorator)), const Size(800.0, 56.0)); + expect(tester.getTopLeft(find.text('label')).dy, 12.0); + expect(tester.getBottomLeft(find.text('label')).dy, 24.0); + expect(getBorderBottom(tester), 56.0); + expect(getBorderWeight(tester), 2.0); + + // Verify that the labelStyle was used + expect(getLabelStyle(tester).color, labelStyle.color); + }); }