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.
This commit is contained in:
Mahdi 2020-12-26 11:07:36 +03:30 committed by GitHub
parent 5f46931a8f
commit 8f5d0371af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 0 deletions

View File

@ -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,
@ -2509,6 +2510,7 @@ class InputDecoration {
this.helperMaxLines,
this.hintText,
this.hintStyle,
this.hintTextDirection,
this.hintMaxLines,
this.errorText,
this.errorStyle,
@ -2566,6 +2568,7 @@ class InputDecoration {
this.hasFloatingPlaceholder = true,
this.floatingLabelBehavior,
this.hintStyle,
this.hintTextDirection,
this.filled = false,
this.fillColor,
this.focusColor,
@ -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.
@ -3307,6 +3316,7 @@ class InputDecoration {
int? helperMaxLines,
String? hintText,
TextStyle? hintStyle,
TextDirection? hintTextDirection,
int? hintMaxLines,
String? errorText,
TextStyle? errorStyle,
@ -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,
@ -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
@ -3488,6 +3500,7 @@ class InputDecoration {
helperMaxLines,
hintText,
hintStyle,
hintTextDirection,
hintMaxLines,
errorText,
errorStyle,

View File

@ -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);
});
}