Skip to content

Commit 1275194

Browse files
authored
Fix TextField does not inherit local InputDecorationTheme (#176300)
## Description This PR replaces global `ThemeData.inputDecorationTheme` usage in `TextField` with `InputDecorationTheme.of ` which returns the ambient `InputDecorationTheme`. It is a follow up to flutter/flutter#168981 which introduces `InputDecorationTheme.of `. ## Related Issue Fixes [[SearchBar] Inconsistent inheritance of InputDecorationTheme](flutter/flutter#176198) ## Tests - Adds 1 test - Updates 1 test
1 parent 45637be commit 1275194

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,14 +1193,13 @@ class _TextFieldState extends State<TextField>
11931193
InputDecoration _getEffectiveDecoration() {
11941194
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
11951195
final ThemeData themeData = Theme.of(context);
1196+
final InputDecorationThemeData decorationTheme = InputDecorationTheme.of(context);
11961197
final InputDecoration effectiveDecoration = (widget.decoration ?? const InputDecoration())
1197-
.applyDefaults(themeData.inputDecorationTheme)
1198+
.applyDefaults(decorationTheme)
11981199
.copyWith(
11991200
enabled: _isEnabled,
12001201
hintMaxLines:
1201-
widget.decoration?.hintMaxLines ??
1202-
themeData.inputDecorationTheme.hintMaxLines ??
1203-
widget.maxLines,
1202+
widget.decoration?.hintMaxLines ?? decorationTheme.hintMaxLines ?? widget.maxLines,
12041203
);
12051204

12061205
// No need to build anything if counter or counterText were given directly.

packages/flutter/test/material/text_field_test.dart

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15557,26 +15557,66 @@ void main() {
1555715557
});
1555815558

1555915559
// Regression test for https://github.com/flutter/flutter/issues/140607.
15560-
testWidgets('TextFields can inherit errorStyle color from InputDecorationThemeData.', (
15560+
testWidgets('TextFields can inherit errorStyle color from InputDecorationThemeData', (
1556115561
WidgetTester tester,
1556215562
) async {
15563-
Widget textFieldBuilder() {
15564-
return MaterialApp(
15565-
theme: ThemeData(
15566-
inputDecorationTheme: const InputDecorationThemeData(
15567-
errorStyle: TextStyle(color: Colors.green),
15568-
),
15569-
),
15563+
const InputDecorationThemeData decorationTheme = InputDecorationThemeData(
15564+
errorStyle: TextStyle(color: Colors.green),
15565+
);
15566+
15567+
EditableTextState getEditableTextState() {
15568+
return tester.state<EditableTextState>(find.byType(EditableText));
15569+
}
15570+
15571+
// Global theme.
15572+
await tester.pumpWidget(
15573+
MaterialApp(
15574+
theme: ThemeData(inputDecorationTheme: decorationTheme),
1557015575
home: const Scaffold(
1557115576
body: TextField(decoration: InputDecoration(errorText: 'error')),
1557215577
),
15573-
);
15574-
}
15578+
),
15579+
);
15580+
expect(getEditableTextState().widget.cursorColor, Colors.green);
1557515581

15576-
await tester.pumpWidget(textFieldBuilder());
15577-
await tester.pumpAndSettle();
15578-
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
15579-
expect(state.widget.cursorColor, Colors.green);
15582+
// Local theme.
15583+
await tester.pumpWidget(
15584+
const MaterialApp(
15585+
home: Scaffold(
15586+
body: InputDecorationTheme(
15587+
data: decorationTheme,
15588+
child: TextField(decoration: InputDecoration(errorText: 'error')),
15589+
),
15590+
),
15591+
),
15592+
);
15593+
expect(getEditableTextState().widget.cursorColor, Colors.green);
15594+
});
15595+
15596+
testWidgets('TextField can inherit decoration from local InputDecorationThemeData', (
15597+
WidgetTester tester,
15598+
) async {
15599+
const InputDecoration decoration = InputDecoration(labelText: 'Label');
15600+
const InputDecorationThemeData decorationTheme = InputDecorationThemeData(
15601+
errorStyle: TextStyle(color: Colors.green),
15602+
);
15603+
15604+
await tester.pumpWidget(
15605+
const MaterialApp(
15606+
home: Scaffold(
15607+
body: InputDecorationTheme(
15608+
data: decorationTheme,
15609+
child: TextField(decoration: decoration),
15610+
),
15611+
),
15612+
),
15613+
);
15614+
15615+
final InputDecorator decorator = tester.widget(find.byType(InputDecorator));
15616+
final InputDecoration expectedDecoration = decoration
15617+
.applyDefaults(decorationTheme)
15618+
.copyWith(enabled: true, hintMaxLines: 1);
15619+
expect(decorator.decoration, expectedDecoration);
1558015620
});
1558115621

1558215622
group('MaxLengthEnforcement', () {

0 commit comments

Comments
 (0)