From 79862a72622aeb03166abdd36c7cba4d3b270272 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Thu, 2 Nov 2017 16:42:02 -0700 Subject: [PATCH] Fixing assumption that baseStyle is a complete style. (#12836) This fixes the assumption that InputDecorator had where it expected baseStyle to be a complete style. Now it merges the baseStyle with the subhead style instead of substituting it entirely. This fixes #12832. --- .../lib/src/material/input_decorator.dart | 15 ++-- .../test/material/input_decorator_test.dart | 75 +++++++++++++++++++ .../test/material/text_field_test.dart | 4 + 3 files changed, 86 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index c6f1614c29..ea9de54aaa 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -472,23 +472,21 @@ class InputDecorator extends StatelessWidget { final Color activeColor = _getActiveColor(themeData); - final TextStyle baseStyle = this.baseStyle ?? themeData.textTheme.subhead; - final TextStyle hintStyle = decoration.hintStyle ?? baseStyle.copyWith(color: themeData.hintColor); - final TextStyle helperStyle = decoration.helperStyle ?? themeData.textTheme.caption.copyWith(color: themeData.hintColor); - final TextStyle counterStyle = decoration.counterStyle ?? helperStyle; + final TextStyle baseStyle = themeData.textTheme.subhead.merge(this.baseStyle); + final TextStyle hintStyle = baseStyle.copyWith(color: themeData.hintColor).merge(decoration.hintStyle); + final TextStyle helperStyle = themeData.textTheme.caption.copyWith(color: themeData.hintColor).merge(decoration.helperStyle); + final TextStyle counterStyle = helperStyle.merge(decoration.counterStyle); final TextStyle subtextStyle = errorText != null - ? decoration.errorStyle ?? themeData.textTheme.caption.copyWith(color: themeData.errorColor) + ? themeData.textTheme.caption.copyWith(color: themeData.errorColor).merge(decoration.errorStyle) : helperStyle; - final double entryTextHeight = baseStyle.fontSize * textScaleFactor; - double topPadding = isCollapsed ? 0.0 : (isDense ? _kDenseTopPadding : _kNormalTopPadding); final List stackChildren = []; if (labelText != null) { assert(!isCollapsed); - final TextStyle floatingLabelStyle = decoration.labelStyle ?? themeData.textTheme.caption.copyWith(color: activeColor); + final TextStyle floatingLabelStyle = themeData.textTheme.caption.copyWith(color: activeColor).merge(decoration.labelStyle); final TextStyle labelStyle = hasInlineLabel ? hintStyle : floatingLabelStyle; final double labelTextHeight = floatingLabelStyle.fontSize * textScaleFactor; @@ -645,6 +643,7 @@ class InputDecorator extends StatelessWidget { if (decoration.icon != null) { assert(!isCollapsed); final double iconSize = isDense ? 18.0 : 24.0; + final double entryTextHeight = baseStyle.fontSize * textScaleFactor; final double iconTop = topPadding + (entryTextHeight - iconSize) / 2.0; return new Row( crossAxisAlignment: CrossAxisAlignment.start, diff --git a/packages/flutter/test/material/input_decorator_test.dart b/packages/flutter/test/material/input_decorator_test.dart index c9c05fdc3a..e74ea27498 100644 --- a/packages/flutter/test/material/input_decorator_test.dart +++ b/packages/flutter/test/material/input_decorator_test.dart @@ -436,4 +436,79 @@ void main() { 'InputDecorator-[<\'key\'>](decoration: InputDecoration(), baseStyle: TextStyle(), isFocused: false, isEmpty: false)', ); }); + + testWidgets('InputDecorator works with partially specified styles', (WidgetTester tester) async { + await tester.pumpWidget(new MaterialApp( + home: const Material( + child: const DefaultTextStyle( + style: const TextStyle(fontFamily: 'Ahem', fontSize: 10.0), + child: const Center( + child: const TextField( + decoration: const InputDecoration( + labelText: 'label', + labelStyle: const TextStyle(), + helperText: 'helper', + helperStyle: const TextStyle(), + hintText: 'hint', + hintStyle: const TextStyle(), + errorText: 'error', + errorStyle: const TextStyle(), + prefixText: 'prefix', + prefixStyle: const TextStyle(), + suffixText: 'suffix', + suffixStyle: const TextStyle(), + counterText: 'counter', + counterStyle: const TextStyle(), + ), + ), + ), + ), + ), + )); + + expect(find.text('label'), findsOneWidget); + + // Tap to make the hint show up. + await tester.tap(find.byType(TextField)); + await tester.pump(const Duration(milliseconds: 100)); + + expect(find.text('hint'), findsOneWidget); + + // Enter text to make the text style get used. + await tester.enterText(find.byType(TextField), 'Test'); + await tester.pump(const Duration(milliseconds: 100)); + + expect(find.text('prefix'), findsOneWidget); + expect(find.text('suffix'), findsOneWidget); + + // Test again without error, so helper style gets used. + await tester.pumpWidget(new MaterialApp( + home: const Material( + child: const DefaultTextStyle( + style: const TextStyle(), + child: const Center( + child: const TextField( + decoration: const InputDecoration( + labelText: 'label', + labelStyle: const TextStyle(), + helperText: 'helper', + helperStyle: const TextStyle(), + hintText: 'hint', + hintStyle: const TextStyle(), + prefixText: 'prefix', + prefixStyle: const TextStyle(), + suffixText: 'suffix', + suffixStyle: const TextStyle(), + counterText: 'counter', + counterStyle: const TextStyle(), + ), + ), + ), + ), + ), + )); + + expect(find.text('label'), findsOneWidget); + expect(find.text('helper'), findsOneWidget); + }); } diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 1a846b2d3d..6b536c3d91 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -817,6 +817,7 @@ void main() { testWidgets('TextField with specified helperStyle', (WidgetTester tester) async { final TextStyle style = new TextStyle( + inherit: false, color: Colors.pink[500], fontSize: 10.0, ); @@ -865,6 +866,7 @@ void main() { testWidgets('TextField with specified hintStyle', (WidgetTester tester) async { final TextStyle hintStyle = new TextStyle( + inherit: false, color: Colors.pink[500], fontSize: 10.0, ); @@ -886,6 +888,7 @@ void main() { testWidgets('TextField with specified prefixStyle', (WidgetTester tester) async { final TextStyle prefixStyle = new TextStyle( + inherit: false, color: Colors.pink[500], fontSize: 10.0, ); @@ -973,6 +976,7 @@ void main() { testWidgets('TextField prefix and suffix appear correctly with hint text', (WidgetTester tester) async { final TextStyle hintStyle = new TextStyle( + inherit: false, color: Colors.pink[500], fontSize: 10.0, );