Skip to content

Commit

Permalink
TextField's hintText should support TextDirection. (flutter#69534)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
xclud authored Dec 26, 2020
1 parent 5f46931 commit 8f5d037
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/flutter/lib/src/material/input_decorator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2164,6 +2164,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
child: Text(
decoration!.hintText!,
style: hintStyle,
textDirection: decoration!.hintTextDirection,
overflow: TextOverflow.ellipsis,
textAlign: textAlign,
maxLines: decoration!.hintMaxLines,
Expand Down Expand Up @@ -2509,6 +2510,7 @@ class InputDecoration {
this.helperMaxLines,
this.hintText,
this.hintStyle,
this.hintTextDirection,
this.hintMaxLines,
this.errorText,
this.errorStyle,
Expand Down Expand Up @@ -2566,6 +2568,7 @@ class InputDecoration {
this.hasFloatingPlaceholder = true,
this.floatingLabelBehavior,
this.hintStyle,
this.hintTextDirection,
this.filled = false,
this.fillColor,
this.focusColor,
Expand Down Expand Up @@ -2687,6 +2690,12 @@ class InputDecoration {
/// input field and the current [Theme].
final TextStyle? hintStyle;

/// The direction to use for the [hintText].
///
/// If null, defaults to a value derived from [Directionality] for the
/// input field and the current context.
final TextDirection? hintTextDirection;

/// The maximum number of lines the [hintText] can occupy.
///
/// Defaults to the value of [TextField.maxLines] attribute.
Expand Down Expand Up @@ -3307,6 +3316,7 @@ class InputDecoration {
int? helperMaxLines,
String? hintText,
TextStyle? hintStyle,
TextDirection? hintTextDirection,
int? hintMaxLines,
String? errorText,
TextStyle? errorStyle,
Expand Down Expand Up @@ -3352,6 +3362,7 @@ class InputDecoration {
helperMaxLines : helperMaxLines ?? this.helperMaxLines,
hintText: hintText ?? this.hintText,
hintStyle: hintStyle ?? this.hintStyle,
hintTextDirection: hintTextDirection ?? this.hintTextDirection,
hintMaxLines: hintMaxLines ?? this.hintMaxLines,
errorText: errorText ?? this.errorText,
errorStyle: errorStyle ?? this.errorStyle,
Expand Down Expand Up @@ -3440,6 +3451,7 @@ class InputDecoration {
&& other.helperMaxLines == helperMaxLines
&& other.hintText == hintText
&& other.hintStyle == hintStyle
&& other.hintTextDirection == hintTextDirection
&& other.hintMaxLines == hintMaxLines
&& other.errorText == errorText
&& other.errorStyle == errorStyle
Expand Down Expand Up @@ -3488,6 +3500,7 @@ class InputDecoration {
helperMaxLines,
hintText,
hintStyle,
hintTextDirection,
hintMaxLines,
errorText,
errorStyle,
Expand Down
42 changes: 42 additions & 0 deletions packages/flutter/test/material/text_form_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,46 @@ void main() {
final TextField widget = tester.widget(find.byType(TextField));
expect(widget.selectionControls, equals(materialTextSelectionControls));
});

testWidgets('TextFormField respects hintTextDirection', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Material(
child: Directionality(
textDirection: TextDirection.rtl,
child: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Some Label',
hintText: 'Some Hint',
hintTextDirection: TextDirection.ltr,
),
),
),
),
));

final Finder hintTextFinder = find.text('Some Hint');

final Text hintText = tester.firstWidget(hintTextFinder);
expect(hintText.textDirection, TextDirection.ltr);

await tester.pumpWidget(MaterialApp(
home: Material(
child: Directionality(
textDirection: TextDirection.rtl,
child: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Some Label',
hintText: 'Some Hint',
),
),
),
),
));

final BuildContext context = tester.element(hintTextFinder);
final TextDirection textDirection = Directionality.of(context);
expect(textDirection, TextDirection.rtl);
});
}

0 comments on commit 8f5d037

Please sign in to comment.