Unverified Commit 8f5d0371 authored by Mahdi's avatar Mahdi Committed by GitHub

TextField's hintText should support TextDirection. (#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.
parent 5f46931a
...@@ -2164,6 +2164,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat ...@@ -2164,6 +2164,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
child: Text( child: Text(
decoration!.hintText!, decoration!.hintText!,
style: hintStyle, style: hintStyle,
textDirection: decoration!.hintTextDirection,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
textAlign: textAlign, textAlign: textAlign,
maxLines: decoration!.hintMaxLines, maxLines: decoration!.hintMaxLines,
...@@ -2509,6 +2510,7 @@ class InputDecoration { ...@@ -2509,6 +2510,7 @@ class InputDecoration {
this.helperMaxLines, this.helperMaxLines,
this.hintText, this.hintText,
this.hintStyle, this.hintStyle,
this.hintTextDirection,
this.hintMaxLines, this.hintMaxLines,
this.errorText, this.errorText,
this.errorStyle, this.errorStyle,
...@@ -2566,6 +2568,7 @@ class InputDecoration { ...@@ -2566,6 +2568,7 @@ class InputDecoration {
this.hasFloatingPlaceholder = true, this.hasFloatingPlaceholder = true,
this.floatingLabelBehavior, this.floatingLabelBehavior,
this.hintStyle, this.hintStyle,
this.hintTextDirection,
this.filled = false, this.filled = false,
this.fillColor, this.fillColor,
this.focusColor, this.focusColor,
...@@ -2687,6 +2690,12 @@ class InputDecoration { ...@@ -2687,6 +2690,12 @@ class InputDecoration {
/// input field and the current [Theme]. /// input field and the current [Theme].
final TextStyle? hintStyle; 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. /// The maximum number of lines the [hintText] can occupy.
/// ///
/// Defaults to the value of [TextField.maxLines] attribute. /// Defaults to the value of [TextField.maxLines] attribute.
...@@ -3307,6 +3316,7 @@ class InputDecoration { ...@@ -3307,6 +3316,7 @@ class InputDecoration {
int? helperMaxLines, int? helperMaxLines,
String? hintText, String? hintText,
TextStyle? hintStyle, TextStyle? hintStyle,
TextDirection? hintTextDirection,
int? hintMaxLines, int? hintMaxLines,
String? errorText, String? errorText,
TextStyle? errorStyle, TextStyle? errorStyle,
...@@ -3352,6 +3362,7 @@ class InputDecoration { ...@@ -3352,6 +3362,7 @@ class InputDecoration {
helperMaxLines : helperMaxLines ?? this.helperMaxLines, helperMaxLines : helperMaxLines ?? this.helperMaxLines,
hintText: hintText ?? this.hintText, hintText: hintText ?? this.hintText,
hintStyle: hintStyle ?? this.hintStyle, hintStyle: hintStyle ?? this.hintStyle,
hintTextDirection: hintTextDirection ?? this.hintTextDirection,
hintMaxLines: hintMaxLines ?? this.hintMaxLines, hintMaxLines: hintMaxLines ?? this.hintMaxLines,
errorText: errorText ?? this.errorText, errorText: errorText ?? this.errorText,
errorStyle: errorStyle ?? this.errorStyle, errorStyle: errorStyle ?? this.errorStyle,
...@@ -3440,6 +3451,7 @@ class InputDecoration { ...@@ -3440,6 +3451,7 @@ class InputDecoration {
&& other.helperMaxLines == helperMaxLines && other.helperMaxLines == helperMaxLines
&& other.hintText == hintText && other.hintText == hintText
&& other.hintStyle == hintStyle && other.hintStyle == hintStyle
&& other.hintTextDirection == hintTextDirection
&& other.hintMaxLines == hintMaxLines && other.hintMaxLines == hintMaxLines
&& other.errorText == errorText && other.errorText == errorText
&& other.errorStyle == errorStyle && other.errorStyle == errorStyle
...@@ -3488,6 +3500,7 @@ class InputDecoration { ...@@ -3488,6 +3500,7 @@ class InputDecoration {
helperMaxLines, helperMaxLines,
hintText, hintText,
hintStyle, hintStyle,
hintTextDirection,
hintMaxLines, hintMaxLines,
errorText, errorText,
errorStyle, errorStyle,
......
...@@ -581,4 +581,46 @@ void main() { ...@@ -581,4 +581,46 @@ void main() {
final TextField widget = tester.widget(find.byType(TextField)); final TextField widget = tester.widget(find.byType(TextField));
expect(widget.selectionControls, equals(materialTextSelectionControls)); 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);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment