Skip to content

Commit 533c6b2

Browse files
authored
Show hint when label is floating (flutter#60394)
1 parent e3ad034 commit 533c6b2

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -2086,9 +2086,14 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
20862086
}
20872087

20882088
// True if the label will be shown and the hint will not.
2089-
// If we're not focused, there's no value, and labelText was provided,
2090-
// then the label appears where the hint would.
2091-
bool get _hasInlineLabel => !widget._labelShouldWithdraw && decoration.labelText != null;
2089+
// If we're not focused, there's no value, labelText was provided, and
2090+
// floatingLabelBehavior isn't set to always, then the label appears where the
2091+
// hint would.
2092+
bool get _hasInlineLabel {
2093+
return !widget._labelShouldWithdraw
2094+
&& decoration.labelText != null
2095+
&& decoration.floatingLabelBehavior != FloatingLabelBehavior.always;
2096+
}
20922097

20932098
// If the label is a floating placeholder, it's always shown.
20942099
bool get _shouldShowLabel => _hasInlineLabel || _floatingLabelEnabled;

packages/flutter/test/material/input_decorator_test.dart

+17
Original file line numberDiff line numberDiff line change
@@ -4068,6 +4068,23 @@ void main() {
40684068
expect(tester.getTopLeft(find.text('label')).dy, 20.0);
40694069
});
40704070

4071+
testWidgets('InputDecorator hint is displayed when floatingLabelBehavior is always', (WidgetTester tester) async {
4072+
await tester.pumpWidget(
4073+
buildInputDecorator(
4074+
// isFocused: false (default)
4075+
isEmpty: true,
4076+
decoration: const InputDecoration(
4077+
floatingLabelBehavior: FloatingLabelBehavior.always,
4078+
hintText: 'hint',
4079+
labelText: 'label',
4080+
),
4081+
),
4082+
);
4083+
await tester.pumpAndSettle();
4084+
4085+
expect(getOpacity(tester, 'hint'), 1.0);
4086+
});
4087+
40714088
testWidgets('InputDecorator floating label width scales when focused', (WidgetTester tester) async {
40724089
final String longStringA = String.fromCharCodes(List<int>.generate(200, (_) => 65));
40734090
final String longStringB = String.fromCharCodes(List<int>.generate(200, (_) => 66));

0 commit comments

Comments
 (0)