Text field style merge (#24449)

* Merge TextField style with global style so that style changes don't override unspecified fields

* Test that a style param is merged with the theme

* Test a few more style properties

* Analysis fix
This commit is contained in:
Justin McCandless 2018-12-13 08:32:55 -08:00 committed by GitHub
parent 92efec3998
commit 952d24bf1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 1 deletions

View file

@ -575,7 +575,7 @@ class _TextFieldState extends State<TextField> with AutomaticKeepAliveClientMixi
);
final ThemeData themeData = Theme.of(context);
final TextStyle style = widget.style ?? themeData.textTheme.subhead;
final TextStyle style = themeData.textTheme.subhead.merge(widget.style);
final Brightness keyboardAppearance = widget.keyboardAppearance ?? themeData.primaryColorBrightness;
final TextEditingController controller = _effectiveController;
final FocusNode focusNode = _effectiveFocusNode;

View file

@ -3420,6 +3420,56 @@ void main() {
expect(tapCount, 0);
});
testWidgets('TextField style is merged with theme', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/23994
final ThemeData themeData = ThemeData(
textTheme: TextTheme(
subhead: TextStyle(
color: Colors.blue[500],
),
),
);
Widget buildFrame(TextStyle style) {
return MaterialApp(
theme: themeData,
home: Material(
child: Center(
child: TextField(
style: style,
),
),
),
);
}
// Empty TextStyle is overridden by theme
await tester.pumpWidget(buildFrame(const TextStyle()));
EditableText editableText = tester.widget(find.byType(EditableText));
expect(editableText.style.color, themeData.textTheme.subhead.color);
expect(editableText.style.background, themeData.textTheme.subhead.background);
expect(editableText.style.shadows, themeData.textTheme.subhead.shadows);
expect(editableText.style.decoration, themeData.textTheme.subhead.decoration);
expect(editableText.style.locale, themeData.textTheme.subhead.locale);
expect(editableText.style.wordSpacing, themeData.textTheme.subhead.wordSpacing);
// Properties set on TextStyle override theme
const Color setColor = Colors.red;
await tester.pumpWidget(buildFrame(const TextStyle(color: setColor)));
editableText = tester.widget(find.byType(EditableText));
expect(editableText.style.color, setColor);
// inherit: false causes nothing to be merged in from theme
await tester.pumpWidget(buildFrame(const TextStyle(
fontSize: 24.0,
textBaseline: TextBaseline.alphabetic,
inherit: false,
)));
editableText = tester.widget(find.byType(EditableText));
expect(editableText.style.color, isNull);
});
testWidgets('style enforces required fields', (WidgetTester tester) async {
Widget buildFrame(TextStyle style) {
return MaterialApp(