Skip to content

Commit 8f5d037

Browse files
authored
TextField's hintText should support TextDirection. (flutter#69534)
* TextField's hintText should support TextDirection. There are many cases for RTL languages that the TextField's label is RTL but the input direction is LTR (e.g. email address). Therefore we may need to change the directionality of the hintText. * Update input_decorator.dart * Adds hintTextDirection tests. * React to reviewer's comments. * Fixes two more analysis issues.
1 parent 5f46931 commit 8f5d037

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
21642164
child: Text(
21652165
decoration!.hintText!,
21662166
style: hintStyle,
2167+
textDirection: decoration!.hintTextDirection,
21672168
overflow: TextOverflow.ellipsis,
21682169
textAlign: textAlign,
21692170
maxLines: decoration!.hintMaxLines,
@@ -2509,6 +2510,7 @@ class InputDecoration {
25092510
this.helperMaxLines,
25102511
this.hintText,
25112512
this.hintStyle,
2513+
this.hintTextDirection,
25122514
this.hintMaxLines,
25132515
this.errorText,
25142516
this.errorStyle,
@@ -2566,6 +2568,7 @@ class InputDecoration {
25662568
this.hasFloatingPlaceholder = true,
25672569
this.floatingLabelBehavior,
25682570
this.hintStyle,
2571+
this.hintTextDirection,
25692572
this.filled = false,
25702573
this.fillColor,
25712574
this.focusColor,
@@ -2687,6 +2690,12 @@ class InputDecoration {
26872690
/// input field and the current [Theme].
26882691
final TextStyle? hintStyle;
26892692

2693+
/// The direction to use for the [hintText].
2694+
///
2695+
/// If null, defaults to a value derived from [Directionality] for the
2696+
/// input field and the current context.
2697+
final TextDirection? hintTextDirection;
2698+
26902699
/// The maximum number of lines the [hintText] can occupy.
26912700
///
26922701
/// Defaults to the value of [TextField.maxLines] attribute.
@@ -3307,6 +3316,7 @@ class InputDecoration {
33073316
int? helperMaxLines,
33083317
String? hintText,
33093318
TextStyle? hintStyle,
3319+
TextDirection? hintTextDirection,
33103320
int? hintMaxLines,
33113321
String? errorText,
33123322
TextStyle? errorStyle,
@@ -3352,6 +3362,7 @@ class InputDecoration {
33523362
helperMaxLines : helperMaxLines ?? this.helperMaxLines,
33533363
hintText: hintText ?? this.hintText,
33543364
hintStyle: hintStyle ?? this.hintStyle,
3365+
hintTextDirection: hintTextDirection ?? this.hintTextDirection,
33553366
hintMaxLines: hintMaxLines ?? this.hintMaxLines,
33563367
errorText: errorText ?? this.errorText,
33573368
errorStyle: errorStyle ?? this.errorStyle,
@@ -3440,6 +3451,7 @@ class InputDecoration {
34403451
&& other.helperMaxLines == helperMaxLines
34413452
&& other.hintText == hintText
34423453
&& other.hintStyle == hintStyle
3454+
&& other.hintTextDirection == hintTextDirection
34433455
&& other.hintMaxLines == hintMaxLines
34443456
&& other.errorText == errorText
34453457
&& other.errorStyle == errorStyle
@@ -3488,6 +3500,7 @@ class InputDecoration {
34883500
helperMaxLines,
34893501
hintText,
34903502
hintStyle,
3503+
hintTextDirection,
34913504
hintMaxLines,
34923505
errorText,
34933506
errorStyle,

packages/flutter/test/material/text_form_field_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,46 @@ void main() {
581581
final TextField widget = tester.widget(find.byType(TextField));
582582
expect(widget.selectionControls, equals(materialTextSelectionControls));
583583
});
584+
585+
testWidgets('TextFormField respects hintTextDirection', (WidgetTester tester) async {
586+
await tester.pumpWidget(MaterialApp(
587+
home: Material(
588+
child: Directionality(
589+
textDirection: TextDirection.rtl,
590+
child: TextFormField(
591+
decoration: const InputDecoration(
592+
border: OutlineInputBorder(),
593+
labelText: 'Some Label',
594+
hintText: 'Some Hint',
595+
hintTextDirection: TextDirection.ltr,
596+
),
597+
),
598+
),
599+
),
600+
));
601+
602+
final Finder hintTextFinder = find.text('Some Hint');
603+
604+
final Text hintText = tester.firstWidget(hintTextFinder);
605+
expect(hintText.textDirection, TextDirection.ltr);
606+
607+
await tester.pumpWidget(MaterialApp(
608+
home: Material(
609+
child: Directionality(
610+
textDirection: TextDirection.rtl,
611+
child: TextFormField(
612+
decoration: const InputDecoration(
613+
border: OutlineInputBorder(),
614+
labelText: 'Some Label',
615+
hintText: 'Some Hint',
616+
),
617+
),
618+
),
619+
),
620+
));
621+
622+
final BuildContext context = tester.element(hintTextFinder);
623+
final TextDirection textDirection = Directionality.of(context);
624+
expect(textDirection, TextDirection.rtl);
625+
});
584626
}

0 commit comments

Comments
 (0)