diff --git a/packages/flutter/lib/src/material/search.dart b/packages/flutter/lib/src/material/search.dart index 18d77a3638..8062079eff 100644 --- a/packages/flutter/lib/src/material/search.dart +++ b/packages/flutter/lib/src/material/search.dart @@ -180,7 +180,7 @@ abstract class SearchDelegate { /// See also: /// /// * [AppBar.leading], the intended use for the return value of this method. - Widget buildLeading(BuildContext context); + Widget? buildLeading(BuildContext context); /// Widgets to display after the search query in the [AppBar]. /// @@ -193,7 +193,7 @@ abstract class SearchDelegate { /// See also: /// /// * [AppBar.actions], the intended use for the return value of this method. - List buildActions(BuildContext context); + List? buildActions(BuildContext context); /// Widget to display across the bottom of the [AppBar]. /// diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index a38450a2eb..a07a585a19 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -771,6 +771,48 @@ void main() { expect(textField.style!.color, themeData.textTheme.bodyText1!.color); expect(textField.style!.color, isNot(equals(themeData.primaryColor))); }); + + // Regression test for: https://github.com/flutter/flutter/issues/78144 + testWidgets('`Leading` and `Actions` nullable test', (WidgetTester tester) async { + // The search delegate page is displayed with no issues + // even with a null return values for [buildLeading] and [buildActions]. + final _TestEmptySearchDelegate delegate = _TestEmptySearchDelegate( + result: 'Result', + suggestions: 'Suggestions', + ); + final List selectedResults = []; + + await tester.pumpWidget(TestHomePage( + delegate: delegate, + results: selectedResults, + )); + + // We are on the homepage. + expect(find.text('HomeBody'), findsOneWidget); + expect(find.text('HomeTitle'), findsOneWidget); + expect(find.text('Suggestions'), findsNothing); + + // Open the search page. + await tester.tap(find.byTooltip('Search')); + await tester.pumpAndSettle(); + + expect(find.text('HomeBody'), findsNothing); + expect(find.text('HomeTitle'), findsNothing); + expect(find.text('Suggestions'), findsOneWidget); + expect(selectedResults, hasLength(0)); + + final TextField textField = tester.widget(find.byType(TextField)); + expect(textField.focusNode!.hasFocus, isTrue); + + // Close the search page. + await tester.tap(find.byTooltip('Close')); + await tester.pumpAndSettle(); + + expect(find.text('HomeBody'), findsOneWidget); + expect(find.text('HomeTitle'), findsOneWidget); + expect(find.text('Suggestions'), findsNothing); + expect(selectedResults, ['Result']); + }); } class TestHomePage extends StatelessWidget { @@ -911,3 +953,48 @@ class _TestSearchDelegate extends SearchDelegate { ); } } + +class _TestEmptySearchDelegate extends SearchDelegate { + _TestEmptySearchDelegate({ + this.suggestions = 'Suggestions', + this.result = 'Result', + }) : super(); + + final String suggestions; + final String result; + + @override + Widget? buildLeading(BuildContext context) => null; + + @override + List? buildActions(BuildContext context) => null; + + @override + Widget buildSuggestions(BuildContext context) { + return MaterialButton( + onPressed: () { + showResults(context); + }, + child: Text(suggestions), + ); + } + + @override + Widget buildResults(BuildContext context) { + return const Text('Results'); + } + + @override + PreferredSizeWidget buildBottom(BuildContext context) { + return PreferredSize( + preferredSize: const Size.fromHeight(56.0), + child: IconButton( + tooltip: 'Close', + icon: const Icon(Icons.arrow_back), + onPressed: () { + close(context, result); + }, + ), + ); + } +}