From 69d763779612cbb014afb9717ca6dcf11393a60b Mon Sep 17 00:00:00 2001 From: Darren Austin Date: Wed, 28 Oct 2020 17:52:05 -0700 Subject: [PATCH] Revert "Updated SearchDelegate to follow custom InputDecorationTheme (#55209)" (#69241) --- packages/flutter/lib/src/material/search.dart | 75 ++++++++----------- .../flutter/test/material/search_test.dart | 73 +++++------------- 2 files changed, 52 insertions(+), 96 deletions(-) diff --git a/packages/flutter/lib/src/material/search.dart b/packages/flutter/lib/src/material/search.dart index f8104d8a8f..7f6e26235e 100644 --- a/packages/flutter/lib/src/material/search.dart +++ b/packages/flutter/lib/src/material/search.dart @@ -92,10 +92,9 @@ Future showSearch({ /// ## Handling emojis and other complex characters /// {@macro flutter.widgets.editableText.complexCharacters} abstract class SearchDelegate { - /// Constructor to be called by subclasses which may specify - /// [searchFieldLabel], either [searchFieldStyle] or [searchFieldDecorationTheme], - /// [keyboardType] and/or [textInputAction]. Only one of [searchFieldLabel] - /// and [searchFieldDecorationTheme] may be non-null. + + /// Constructor to be called by subclasses which may specify [searchFieldLabel], [keyboardType] and/or + /// [textInputAction]. /// /// {@tool snippet} /// ```dart @@ -125,10 +124,9 @@ abstract class SearchDelegate { SearchDelegate({ this.searchFieldLabel, this.searchFieldStyle, - this.searchFieldDecorationTheme, this.keyboardType, this.textInputAction = TextInputAction.search, - }) : assert(searchFieldStyle == null || searchFieldDecorationTheme == null); + }); /// Suggestions shown in the body of the search page while the user types a /// query into the search field. @@ -207,11 +205,6 @@ abstract class SearchDelegate { primaryIconTheme: theme.primaryIconTheme.copyWith(color: Colors.grey), primaryColorBrightness: Brightness.light, primaryTextTheme: theme.textTheme, - inputDecorationTheme: searchFieldDecorationTheme ?? - InputDecorationTheme( - hintStyle: searchFieldStyle ?? theme.inputDecorationTheme.hintStyle, - border: InputBorder.none, - ), ); } @@ -283,18 +276,8 @@ abstract class SearchDelegate { /// /// If this value is set to null, the value of the ambient [Theme]'s /// [InputDecorationTheme.hintStyle] will be used instead. - /// - /// If this value is not null, [searchFieldDecorationTheme] - /// will be ignored so this can be used. final TextStyle? searchFieldStyle; - /// The [InputDecorationTheme] for the search field, use - /// this if you just want to customize the [TextField] - /// - /// If this value is not null, [searchFieldStyle] - /// will be ignored so this can be used. - final InputDecorationTheme? searchFieldDecorationTheme; - /// The type of action button to use for the keyboard. /// /// Defaults to the default value specified in [TextField]. @@ -499,6 +482,8 @@ class _SearchPageState extends State<_SearchPage> { final ThemeData theme = widget.delegate.appBarTheme(context); final String searchFieldLabel = widget.delegate.searchFieldLabel ?? MaterialLocalizations.of(context).searchFieldLabel; + final TextStyle? searchFieldStyle = widget.delegate.searchFieldStyle + ?? theme.inputDecorationTheme.hintStyle; Widget? body; switch(widget.delegate._currentBody) { case _SearchBody.suggestions: @@ -516,8 +501,7 @@ class _SearchPageState extends State<_SearchPage> { case null: break; } - - late final String routeName; + final String routeName; switch (theme.platform) { case TargetPlatform.iOS: case TargetPlatform.macOS: @@ -535,28 +519,33 @@ class _SearchPageState extends State<_SearchPage> { scopesRoute: true, namesRoute: true, label: routeName, - child: Theme( - data: theme, - child: Scaffold( - appBar: AppBar( - leading: widget.delegate.buildLeading(context), - title: TextField( - controller: widget.delegate._queryTextController, - focusNode: focusNode, - style: theme.textTheme.title, - textInputAction: widget.delegate.textInputAction, - keyboardType: widget.delegate.keyboardType, - onSubmitted: (String _) { - widget.delegate.showResults(context); - }, - decoration: InputDecoration(hintText: searchFieldLabel), + child: Scaffold( + appBar: AppBar( + backgroundColor: theme.primaryColor, + iconTheme: theme.primaryIconTheme, + textTheme: theme.primaryTextTheme, + brightness: theme.primaryColorBrightness, + leading: widget.delegate.buildLeading(context), + title: TextField( + controller: widget.delegate._queryTextController, + focusNode: focusNode, + style: theme.textTheme.headline6, + textInputAction: widget.delegate.textInputAction, + keyboardType: widget.delegate.keyboardType, + onSubmitted: (String _) { + widget.delegate.showResults(context); + }, + decoration: InputDecoration( + border: InputBorder.none, + hintText: searchFieldLabel, + hintStyle: searchFieldStyle, ), - actions: widget.delegate.buildActions(context), - ), - body: AnimatedSwitcher( - duration: const Duration(milliseconds: 300), - child: body, ), + actions: widget.delegate.buildActions(context), + ), + body: AnimatedSwitcher( + duration: const Duration(milliseconds: 300), + child: body, ), ), ); diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index e849e3fbc5..eff15bafd1 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -122,8 +122,7 @@ void main() { }); testWidgets('Hint text color overridden', (WidgetTester tester) async { - const String searchHintText = 'Enter search terms'; - final _TestSearchDelegate delegate = _TestSearchDelegate(searchHint: searchHintText); + final _TestSearchDelegate delegate = _TestSearchDelegate(); await tester.pumpWidget(TestHomePage( delegate: delegate, @@ -131,8 +130,9 @@ void main() { await tester.tap(find.byTooltip('Search')); await tester.pumpAndSettle(); - final Text hintText = tester.widget(find.text(searchHintText)); - expect(hintText.style!.color, _TestSearchDelegate.hintTextColor); + final TextField textField = tester.widget(find.byType(TextField)); + final Color hintColor = textField.decoration!.hintStyle!.color!; + expect(hintColor, delegate.hintTextColor); }); testWidgets('Requests suggestions', (WidgetTester tester) async { @@ -540,18 +540,20 @@ void main() { }); testWidgets('Custom searchFieldStyle value', (WidgetTester tester) async { - const String searchHintText = 'Enter search terms'; - const TextStyle searchFieldStyle = TextStyle(color: Colors.red, fontSize: 3); + const TextStyle searchStyle = TextStyle(color: Colors.red, fontSize: 3); - final _TestSearchDelegate delegate = _TestSearchDelegate(searchHint: searchHintText, searchFieldStyle: searchFieldStyle); + final _TestSearchDelegate delegate = _TestSearchDelegate(searchFieldStyle: searchStyle); - await tester.pumpWidget(TestHomePage(delegate: delegate)); + await tester.pumpWidget( + TestHomePage( + delegate: delegate, + )); await tester.tap(find.byTooltip('Search')); await tester.pumpAndSettle(); - final Text hintText = tester.widget(find.text(searchHintText)); - expect(hintText.style?.color, delegate.searchFieldStyle?.color); - expect(hintText.style?.fontSize, delegate.searchFieldStyle?.fontSize); + final TextField textField = tester.widget(find.byType(TextField)); + final TextStyle hintStyle = textField.decoration!.hintStyle!; + expect(hintStyle, delegate.searchFieldStyle); }); testWidgets('keyboard show search button by default', (WidgetTester tester) async { @@ -690,23 +692,6 @@ void main() { }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS })); }); - testWidgets('Custom searchFieldDecorationTheme value', - (WidgetTester tester) async { - const InputDecorationTheme searchFieldDecorationTheme = InputDecorationTheme( - hintStyle: TextStyle(color: _TestSearchDelegate.hintTextColor), - ); - final _TestSearchDelegate delegate = _TestSearchDelegate( - searchFieldDecorationTheme: searchFieldDecorationTheme, - ); - - await tester.pumpWidget(TestHomePage(delegate: delegate)); - await tester.tap(find.byTooltip('Search')); - await tester.pumpAndSettle(); - - final ThemeData? textFieldTheme = Theme.of(tester.element(find.byType(TextField))); - expect(textFieldTheme?.inputDecorationTheme, searchFieldDecorationTheme); - }); - // Regression test for: https://github.com/flutter/flutter/issues/66781 testWidgets('text in search bar contrasts background (light mode)', (WidgetTester tester) async { final ThemeData themeData = ThemeData.light(); @@ -724,11 +709,8 @@ void main() { await tester.tap(find.byTooltip('Search')); await tester.pumpAndSettle(); - final Material appBarBackground = tester.widget(find.descendant( - of: find.byType(AppBar), - matching: find.byType(Material), - )); - expect(appBarBackground.color, Colors.white); + final AppBar appBar = tester.widget(find.byType(AppBar)); + expect(appBar.backgroundColor, Colors.white); final TextField textField = tester.widget(find.byType(TextField)); expect(textField.style!.color, themeData.textTheme.bodyText1!.color); @@ -752,11 +734,8 @@ void main() { await tester.tap(find.byTooltip('Search')); await tester.pumpAndSettle(); - final Material appBarBackground = tester.widget(find.descendant( - of: find.byType(AppBar), - matching: find.byType(Material), - )); - expect(appBarBackground.color, themeData.primaryColor); + final AppBar appBar = tester.widget(find.byType(AppBar)); + expect(appBar.backgroundColor, themeData.primaryColor); final TextField textField = tester.widget(find.byType(TextField)); expect(textField.style!.color, themeData.textTheme.bodyText1!.color); @@ -824,22 +803,16 @@ class _TestSearchDelegate extends SearchDelegate { this.result = 'Result', this.actions = const [], this.defaultAppBarTheme = false, - InputDecorationTheme? searchFieldDecorationTheme, TextStyle? searchFieldStyle, String? searchHint, TextInputAction textInputAction = TextInputAction.search, - }) : super( - searchFieldLabel: searchHint, - textInputAction: textInputAction, - searchFieldStyle: searchFieldStyle, - searchFieldDecorationTheme: searchFieldDecorationTheme, - ); + }) : super(searchFieldLabel: searchHint, textInputAction: textInputAction, searchFieldStyle: searchFieldStyle); final bool defaultAppBarTheme; final String suggestions; final String result; final List actions; - static const Color hintTextColor = Colors.green; + final Color hintTextColor = Colors.green; @override ThemeData appBarTheme(BuildContext context) { @@ -848,13 +821,7 @@ class _TestSearchDelegate extends SearchDelegate { } final ThemeData theme = Theme.of(context)!; return theme.copyWith( - inputDecorationTheme: searchFieldDecorationTheme ?? - InputDecorationTheme( - hintStyle: searchFieldStyle ?? - const TextStyle( - color: hintTextColor, - ), - ), + inputDecorationTheme: InputDecorationTheme(hintStyle: TextStyle(color: hintTextColor)), ); }