Skip to content

Commit 114335d

Browse files
trevorwangxster
authored andcommitted
Support TextField multi-line hint text flutter#20941 (flutter#24976)
1 parent 65df90d commit 114335d

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
233233
TextFormField(
234234
decoration: const InputDecoration(
235235
border: OutlineInputBorder(),
236-
hintText: 'Tell us about yourself',
236+
hintText: 'Tell us about yourself (e.g., write down what you do or what hobbies you have)',
237237
helperText: 'Keep it short, this is just a demo.',
238238
labelText: 'Life story',
239239
),

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
17191719
style: hintStyle,
17201720
overflow: TextOverflow.ellipsis,
17211721
textAlign: textAlign,
1722+
maxLines: decoration.hintMaxLines,
17221723
),
17231724
);
17241725

@@ -1944,6 +1945,7 @@ class InputDecoration {
19441945
this.helperStyle,
19451946
this.hintText,
19461947
this.hintStyle,
1948+
this.hintMaxLines,
19471949
this.errorText,
19481950
this.errorStyle,
19491951
this.errorMaxLines,
@@ -1994,6 +1996,7 @@ class InputDecoration {
19941996
labelStyle = null,
19951997
helperText = null,
19961998
helperStyle = null,
1999+
hintMaxLines = null,
19972000
errorText = null,
19982001
errorStyle = null,
19992002
errorMaxLines = null,
@@ -2081,6 +2084,15 @@ class InputDecoration {
20812084
/// input field and the current [Theme].
20822085
final TextStyle hintStyle;
20832086

2087+
/// The maximum number of lines the [hintText] can occupy.
2088+
///
2089+
/// Defaults to the value of [TextField.maxLines] attribute.
2090+
///
2091+
/// This value is passed along to the [Text.maxLines] attribute
2092+
/// of the [Text] widget used to display the hint text. [TextOverflow.ellipsis] is
2093+
/// used to handle the overflow when it is limited to single line.
2094+
final int hintMaxLines;
2095+
20842096
/// Text that appears below the input [child] and the border.
20852097
///
20862098
/// If non-null, the border's color animates to red and the [helperText] is
@@ -2449,6 +2461,7 @@ class InputDecoration {
24492461
TextStyle helperStyle,
24502462
String hintText,
24512463
TextStyle hintStyle,
2464+
int hintMaxLines,
24522465
String errorText,
24532466
TextStyle errorStyle,
24542467
int errorMaxLines,
@@ -2484,6 +2497,7 @@ class InputDecoration {
24842497
helperStyle: helperStyle ?? this.helperStyle,
24852498
hintText: hintText ?? this.hintText,
24862499
hintStyle: hintStyle ?? this.hintStyle,
2500+
hintMaxLines: hintMaxLines ?? this.hintMaxLines,
24872501
errorText: errorText ?? this.errorText,
24882502
errorStyle: errorStyle ?? this.errorStyle,
24892503
errorMaxLines: errorMaxLines ?? this.errorMaxLines,
@@ -2556,6 +2570,7 @@ class InputDecoration {
25562570
&& typedOther.helperStyle == helperStyle
25572571
&& typedOther.hintText == hintText
25582572
&& typedOther.hintStyle == hintStyle
2573+
&& typedOther.hintMaxLines == hintMaxLines
25592574
&& typedOther.errorText == errorText
25602575
&& typedOther.errorStyle == errorStyle
25612576
&& typedOther.errorMaxLines == errorMaxLines
@@ -2597,6 +2612,7 @@ class InputDecoration {
25972612
helperStyle,
25982613
hintText,
25992614
hintStyle,
2615+
hintMaxLines,
26002616
errorText,
26012617
errorStyle,
26022618
errorMaxLines,
@@ -2646,6 +2662,8 @@ class InputDecoration {
26462662
description.add('helperText: "$helperText"');
26472663
if (hintText != null)
26482664
description.add('hintText: "$hintText"');
2665+
if (hintMaxLines != null)
2666+
description.add('hintMaxLines: "$hintMaxLines"');
26492667
if (errorText != null)
26502668
description.add('errorText: "$errorText"');
26512669
if (errorStyle != null)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ class _TextFieldState extends State<TextField> with AutomaticKeepAliveClientMixi
398398
.applyDefaults(Theme.of(context).inputDecorationTheme)
399399
.copyWith(
400400
enabled: widget.enabled,
401+
hintMaxLines: widget.decoration?.hintMaxLines ?? widget.maxLines
401402
);
402403

403404
if (!needsCounter)

packages/flutter/test/material/text_field_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,43 @@ void main() {
722722
expect(inputBox.size, greaterThan(fourLineInputSize));
723723
});
724724

725+
726+
testWidgets('Multiline hint text will wrap up to maxLines', (WidgetTester tester) async {
727+
final Key textFieldKey = UniqueKey();
728+
729+
Widget builder(int maxLines, final String hintMsg) {
730+
return boilerplate(
731+
child: TextField(
732+
key: textFieldKey,
733+
style: const TextStyle(color: Colors.black, fontSize: 34.0),
734+
maxLines: maxLines,
735+
decoration: InputDecoration(
736+
hintText: hintMsg,
737+
),
738+
),
739+
);
740+
}
741+
742+
const String hintPlaceholder = 'Placeholder';
743+
const String multipleLineText = 'Here\'s a text, which is more than one line, to demostrate the multiple line hint text';
744+
await tester.pumpWidget(builder(null, hintPlaceholder));
745+
746+
RenderBox findHintText(String hint) => tester.renderObject(find.text(hint));
747+
748+
final RenderBox hintTextBox = findHintText(hintPlaceholder);
749+
final Size oneLineHintSize = hintTextBox.size;
750+
751+
await tester.pumpWidget(builder(null, hintPlaceholder));
752+
expect(findHintText(hintPlaceholder), equals(hintTextBox));
753+
expect(hintTextBox.size, equals(oneLineHintSize));
754+
755+
const int maxLines = 3;
756+
await tester.pumpWidget(builder(maxLines, multipleLineText));
757+
final Text hintTextWidget = tester.widget(find.text(multipleLineText));
758+
expect(hintTextWidget.maxLines, equals(maxLines));
759+
expect(findHintText(multipleLineText).size, greaterThan(oneLineHintSize));
760+
});
761+
725762
testWidgets('Can drag handles to change selection in multiline', (WidgetTester tester) async {
726763
final TextEditingController controller = TextEditingController();
727764

0 commit comments

Comments
 (0)