diff --git a/packages/flutter/lib/src/material/search.dart b/packages/flutter/lib/src/material/search.dart index be28a4ab34..7f6e26235e 100644 --- a/packages/flutter/lib/src/material/search.dart +++ b/packages/flutter/lib/src/material/search.dart @@ -199,7 +199,9 @@ abstract class SearchDelegate { final ThemeData theme = Theme.of(context)!; assert(theme != null); return theme.copyWith( - primaryColor: Colors.white, + primaryColor: theme.brightness == Brightness.dark + ? theme.primaryColor + : Colors.white, primaryIconTheme: theme.primaryIconTheme.copyWith(color: Colors.grey), primaryColorBrightness: Brightness.light, primaryTextTheme: theme.textTheme, diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index 9345317e93..eff15bafd1 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -691,6 +691,56 @@ void main() { semantics.dispose(); }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS })); }); + + // 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(); + final _TestSearchDelegate delegate = _TestSearchDelegate( + defaultAppBarTheme: true, + ); + const String query = 'search query'; + await tester.pumpWidget(TestHomePage( + delegate: delegate, + passInInitialQuery: true, + initialQuery: query, + themeData: themeData, + )); + + await tester.tap(find.byTooltip('Search')); + await tester.pumpAndSettle(); + + 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); + expect(textField.style!.color, isNot(equals(Colors.white))); + }); + + // Regression test for: https://github.com/flutter/flutter/issues/66781 + testWidgets('text in search bar contrasts background (dark mode)', (WidgetTester tester) async { + final ThemeData themeData = ThemeData.dark(); + final _TestSearchDelegate delegate = _TestSearchDelegate( + defaultAppBarTheme: true, + ); + const String query = 'search query'; + await tester.pumpWidget(TestHomePage( + delegate: delegate, + passInInitialQuery: true, + initialQuery: query, + themeData: themeData, + )); + + await tester.tap(find.byTooltip('Search')); + await tester.pumpAndSettle(); + + 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); + expect(textField.style!.color, isNot(equals(themeData.primaryColor))); + }); } class TestHomePage extends StatelessWidget { @@ -700,16 +750,19 @@ class TestHomePage extends StatelessWidget { required this.delegate, this.passInInitialQuery = false, this.initialQuery, + this.themeData, }) : super(key: key); final List? results; final SearchDelegate delegate; final bool passInInitialQuery; + final ThemeData? themeData; final String? initialQuery; @override Widget build(BuildContext context) { return MaterialApp( + theme: themeData, home: Builder(builder: (BuildContext context) { return Scaffold( appBar: AppBar( @@ -749,11 +802,13 @@ class _TestSearchDelegate extends SearchDelegate { this.suggestions = 'Suggestions', this.result = 'Result', this.actions = const [], + this.defaultAppBarTheme = false, TextStyle? searchFieldStyle, String? searchHint, TextInputAction textInputAction = TextInputAction.search, }) : super(searchFieldLabel: searchHint, textInputAction: textInputAction, searchFieldStyle: searchFieldStyle); + final bool defaultAppBarTheme; final String suggestions; final String result; final List actions; @@ -761,6 +816,9 @@ class _TestSearchDelegate extends SearchDelegate { @override ThemeData appBarTheme(BuildContext context) { + if (defaultAppBarTheme) { + return super.appBarTheme(context); + } final ThemeData theme = Theme.of(context)!; return theme.copyWith( inputDecorationTheme: InputDecorationTheme(hintStyle: TextStyle(color: hintTextColor)),