Skip to content

Commit e55e7ac

Browse files
authored
[material_ui] Add labelTextDirection handling in InputDecorator (#12607)
This PR introduces `labelTextDirection` property to InputDecoration. Closes #41324 Port of #182477 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 40b7e64 commit e55e7ac

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

‎packages/material_ui/lib/src/input_decorator.dart‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,12 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
23852385
style: labelShouldWithdraw ? _getFloatingLabelStyle(themeData, defaults) : labelStyle,
23862386
child:
23872387
decoration.label ??
2388-
Text(decoration.labelText!, overflow: TextOverflow.ellipsis, textAlign: textAlign),
2388+
Text(
2389+
decoration.labelText!,
2390+
overflow: TextOverflow.ellipsis,
2391+
textAlign: textAlign,
2392+
textDirection: decoration.labelTextDirection,
2393+
),
23892394
),
23902395
),
23912396
);
@@ -2811,6 +2816,7 @@ class InputDecoration {
28112816
this.labelText,
28122817
this.labelStyle,
28132818
this.floatingLabelStyle,
2819+
this.labelTextDirection,
28142820
this.helper,
28152821
this.helperText,
28162822
this.helperStyle,
@@ -2940,6 +2946,7 @@ class InputDecoration {
29402946
labelText = null,
29412947
labelStyle = null,
29422948
floatingLabelStyle = null,
2949+
labelTextDirection = null,
29432950
helper = null,
29442951
helperText = null,
29452952
helperStyle = null,
@@ -3117,6 +3124,17 @@ class InputDecoration {
31173124
/// {@endtemplate}
31183125
final TextStyle? floatingLabelStyle;
31193126

3127+
/// The direction to use for the [labelText].
3128+
///
3129+
/// If null, defaults to a value derived from [Directionality] for the
3130+
/// input field and the current context.
3131+
///
3132+
/// This is ignored if [label] is provided instead of [labelText].
3133+
/// If you need to set the direction for a custom [label] widget,
3134+
/// wrap that widget in a [Directionality] widget or use a [Text]
3135+
/// widget with its own [Text.textDirection] property.
3136+
final TextDirection? labelTextDirection;
3137+
31203138
/// Optional widget that appears below the [InputDecorator.child].
31213139
///
31223140
/// If non-null, the [helper] is displayed below the [InputDecorator.child], in
@@ -3209,6 +3227,11 @@ class InputDecoration {
32093227
///
32103228
/// If null, defaults to a value derived from [Directionality] for the
32113229
/// input field and the current context.
3230+
///
3231+
/// This is ignored if [hint] is provided instead of [hintText].
3232+
/// If you need to set the direction for a custom [hint] widget,
3233+
/// wrap that widget in a [Directionality] widget or use a [Text]
3234+
/// widget with its own [Text.textDirection] property.
32123235
final TextDirection? hintTextDirection;
32133236

32143237
/// The maximum number of lines the [hintText] can occupy.
@@ -3981,6 +4004,7 @@ class InputDecoration {
39814004
String? labelText,
39824005
TextStyle? labelStyle,
39834006
TextStyle? floatingLabelStyle,
4007+
TextDirection? labelTextDirection,
39844008
Widget? helper,
39854009
String? helperText,
39864010
TextStyle? helperStyle,
@@ -4042,6 +4066,7 @@ class InputDecoration {
40424066
labelText: labelText ?? this.labelText,
40434067
labelStyle: labelStyle ?? this.labelStyle,
40444068
floatingLabelStyle: floatingLabelStyle ?? this.floatingLabelStyle,
4069+
labelTextDirection: labelTextDirection ?? this.labelTextDirection,
40454070
helper: helper ?? this.helper,
40464071
helperText: helperText ?? this.helperText,
40474072
helperStyle: helperStyle ?? this.helperStyle,
@@ -4167,6 +4192,7 @@ class InputDecoration {
41674192
other.labelText == labelText &&
41684193
other.labelStyle == labelStyle &&
41694194
other.floatingLabelStyle == floatingLabelStyle &&
4195+
other.labelTextDirection == labelTextDirection &&
41704196
other.helper == helper &&
41714197
other.helperText == helperText &&
41724198
other.helperStyle == helperStyle &&
@@ -4229,6 +4255,7 @@ class InputDecoration {
42294255
label,
42304256
labelText,
42314257
floatingLabelStyle,
4258+
labelTextDirection,
42324259
labelStyle,
42334260
helper,
42344261
helperText,

‎packages/material_ui/lib/src/text_field.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,18 @@ class TextField extends StatefulWidget {
488488
final TextAlignVertical? textAlignVertical;
489489

490490
/// {@macro flutter.widgets.editableText.textDirection}
491+
///
492+
/// This does not affect the direction of the decoration's
493+
/// [InputDecoration.hintText] or [InputDecoration.labelText].
494+
///
495+
/// To override the direction for the entire field (including the hint, label,
496+
/// and prefix/suffix icons), wrap the [TextField] in a [Directionality] widget.
497+
///
498+
/// See also:
499+
///
500+
/// * [InputDecoration.hintTextDirection], which specifically controls the hint.
501+
/// * [InputDecoration.labelTextDirection], which specifically controls the label.
502+
/// * [Directionality], which provides the default direction for the subtree.
491503
final TextDirection? textDirection;
492504

493505
/// {@macro flutter.widgets.editableText.autofocus}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Introduces labelTextDirection property to InputDecoration.
3+
version: minor

‎packages/material_ui/test/input_decorator_test.dart‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15763,4 +15763,39 @@ void main() {
1576315763
);
1576415764
expect(tester.getSize(find.byType(InputDecorator)), Size.zero);
1576515765
});
15766+
15767+
testWidgets('InputDecorator respects individual text directions for label and hint', (
15768+
WidgetTester tester,
15769+
) async {
15770+
const hintText = 'Hint Text';
15771+
const labelText = 'Label Text';
15772+
15773+
await tester.pumpWidget(
15774+
const MaterialApp(
15775+
home: Scaffold(
15776+
body: Directionality(
15777+
textDirection: TextDirection.ltr,
15778+
child: Center(
15779+
child: InputDecorator(
15780+
decoration: InputDecoration(
15781+
hintText: hintText,
15782+
labelText: labelText,
15783+
labelTextDirection: TextDirection.rtl,
15784+
hintTextDirection: TextDirection.rtl,
15785+
),
15786+
isEmpty: true,
15787+
child: SizedBox.shrink(),
15788+
),
15789+
),
15790+
),
15791+
),
15792+
),
15793+
);
15794+
15795+
final Text hint = tester.widget<Text>(find.text(hintText));
15796+
expect(hint.textDirection, TextDirection.rtl);
15797+
15798+
final Text label = tester.widget<Text>(find.text(labelText));
15799+
expect(label.textDirection, TextDirection.rtl);
15800+
});
1576615801
}

0 commit comments

Comments
 (0)