Skip to content

Commit f37a91a

Browse files
Add searchFieldStyle (flutter#54674)
1 parent cf0fcd4 commit f37a91a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ abstract class SearchDelegate<T> {
119119
/// {@end-tool}
120120
SearchDelegate({
121121
this.searchFieldLabel,
122+
this.searchFieldStyle,
122123
this.keyboardType,
123124
this.textInputAction = TextInputAction.search,
124125
});
@@ -264,6 +265,11 @@ abstract class SearchDelegate<T> {
264265
/// If this value is set to null, the value of MaterialLocalizations.of(context).searchFieldLabel will be used instead.
265266
final String searchFieldLabel;
266267

268+
/// The style of the [searchFieldLabel].
269+
///
270+
/// If this value is set to null, the value of the ambient [Theme]'s [ThemeData.inputDecorationTheme.hintStyle] will be used instead.
271+
final TextStyle searchFieldStyle;
272+
267273
/// The type of action button to use for the keyboard.
268274
///
269275
/// Defaults to the default value specified in [TextField].
@@ -315,7 +321,6 @@ enum _SearchBody {
315321
results,
316322
}
317323

318-
319324
class _SearchPageRoute<T> extends PageRoute<T> {
320325
_SearchPageRoute({
321326
@required this.delegate,
@@ -469,6 +474,8 @@ class _SearchPageState<T> extends State<_SearchPage<T>> {
469474
final ThemeData theme = widget.delegate.appBarTheme(context);
470475
final String searchFieldLabel = widget.delegate.searchFieldLabel
471476
?? MaterialLocalizations.of(context).searchFieldLabel;
477+
final TextStyle searchFieldStyle = widget.delegate.searchFieldStyle
478+
?? theme.inputDecorationTheme.hintStyle;
472479
Widget body;
473480
switch(widget.delegate._currentBody) {
474481
case _SearchBody.suggestions:
@@ -521,7 +528,7 @@ class _SearchPageState<T> extends State<_SearchPage<T>> {
521528
decoration: InputDecoration(
522529
border: InputBorder.none,
523530
hintText: searchFieldLabel,
524-
hintStyle: theme.inputDecorationTheme.hintStyle,
531+
hintStyle: searchFieldStyle,
525532
),
526533
),
527534
actions: widget.delegate.buildActions(context),

packages/flutter/test/material/search_test.dart

+19-1
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,23 @@ void main() {
509509
expect(find.text(searchHint), findsOneWidget);
510510
});
511511

512+
testWidgets('Custom searchFieldStyle value', (WidgetTester tester) async {
513+
const TextStyle searchStyle = TextStyle(color: Colors.red, fontSize: 3);
514+
515+
final _TestSearchDelegate delegate = _TestSearchDelegate(searchFieldStyle: searchStyle);
516+
517+
await tester.pumpWidget(
518+
TestHomePage(
519+
delegate: delegate,
520+
));
521+
await tester.tap(find.byTooltip('Search'));
522+
await tester.pumpAndSettle();
523+
524+
final TextField textField = tester.widget<TextField>(find.byType(TextField));
525+
final TextStyle hintStyle = textField.decoration.hintStyle;
526+
expect(hintStyle, delegate.searchFieldStyle);
527+
});
528+
512529
testWidgets('keyboard show search button by default', (WidgetTester tester) async {
513530
final _TestSearchDelegate delegate = _TestSearchDelegate();
514531

@@ -697,9 +714,10 @@ class _TestSearchDelegate extends SearchDelegate<String> {
697714
this.suggestions = 'Suggestions',
698715
this.result = 'Result',
699716
this.actions = const <Widget>[],
717+
TextStyle searchFieldStyle,
700718
String searchHint,
701719
TextInputAction textInputAction = TextInputAction.search,
702-
}) : super(searchFieldLabel: searchHint, textInputAction: textInputAction);
720+
}) : super(searchFieldLabel: searchHint, textInputAction: textInputAction, searchFieldStyle: searchFieldStyle);
703721

704722
final String suggestions;
705723
final String result;

0 commit comments

Comments
 (0)