diff --git a/packages/flutter/lib/src/material/search.dart b/packages/flutter/lib/src/material/search.dart index 1c888f766f..6a09cdb6b6 100644 --- a/packages/flutter/lib/src/material/search.dart +++ b/packages/flutter/lib/src/material/search.dart @@ -48,6 +48,10 @@ import 'theme.dart'; /// route created by this method is pushed to the nearest navigator to the /// given `context`. It can not be `null`. /// +/// The `maintainState` argument is used to determine if the route should remain +/// in memory when it is inactive (see [ModalRoute.maintainState] for more details]. +/// By default, `maintainState` is `false`. +/// /// The transition to the search page triggered by this method looks best if the /// screen triggering the transition contains an [AppBar] at the top and the /// transition is called from an [IconButton] that's part of [AppBar.actions]. @@ -68,11 +72,13 @@ Future showSearch({ required SearchDelegate delegate, String? query = '', bool useRootNavigator = false, + bool maintainState = false, }) { delegate.query = query ?? delegate.query; delegate._currentBody = _SearchBody.suggestions; return Navigator.of(context, rootNavigator: useRootNavigator).push(_SearchPageRoute( delegate: delegate, + maintainState: maintainState )); } @@ -417,6 +423,7 @@ enum _SearchBody { class _SearchPageRoute extends PageRoute { _SearchPageRoute({ required this.delegate, + required this.maintainState, }) { assert( delegate._route == null, @@ -429,6 +436,9 @@ class _SearchPageRoute extends PageRoute { final SearchDelegate delegate; + @override + final bool maintainState; + @override Color? get barrierColor => null; @@ -438,9 +448,6 @@ class _SearchPageRoute extends PageRoute { @override Duration get transitionDuration => const Duration(milliseconds: 300); - @override - bool get maintainState => false; - @override Widget buildTransitions( BuildContext context, diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index 0fbc196e44..d32c7bf7eb 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -1175,6 +1175,63 @@ void main() { await tester.pump(); expect(textField.controller!.text.length, 15); }, skip: kIsWeb); // [intended] We do not use Flutter-rendered context menu on the Web. + + testWidgets('showSearch with maintainState on the route', (WidgetTester tester) async { + final _MyNavigatorObserver navigationObserver = _MyNavigatorObserver(); + + final _TestEmptySearchDelegate delegate = _TestEmptySearchDelegate(); + addTearDown(delegate.dispose); + + await tester.pumpWidget(MaterialApp( + navigatorObservers: [navigationObserver], + home: Builder(builder: (BuildContext context) => Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + TextButton( + onPressed: () async { + await showSearch( + context: context, + delegate: delegate, + ); + }, + child: const Text('showSearch'), + ), + TextButton( + onPressed: () async { + await showSearch( + context: context, + delegate: delegate, + maintainState: true, + ); + }, + child: const Text('showSearchWithMaintainState'), + ), + ], + )), + )); + + expect(navigationObserver.pushCount, 0); + expect(navigationObserver.maintainState, false); + + // showSearch normal and back. + await tester.tap(find.text('showSearch')); + await tester.pumpAndSettle(); + final Finder backButtonFinder = find.byType(BackButton); + expect(backButtonFinder, findsWidgets); + await tester.tap(find.byTooltip('Close')); + await tester.pumpAndSettle(); + expect(navigationObserver.pushCount, 1); + expect(navigationObserver.maintainState, false); + + // showSearch with maintainState. + await tester.tap(find.text('showSearchWithMaintainState')); + await tester.pumpAndSettle(); + expect(backButtonFinder, findsWidgets); + await tester.tap(find.byTooltip('Close')); + await tester.pumpAndSettle(); + expect(navigationObserver.pushCount, 2); + expect(navigationObserver.maintainState, true); + }); } class TestHomePage extends StatelessWidget { @@ -1358,6 +1415,7 @@ class _TestEmptySearchDelegate extends SearchDelegate { } class _MyNavigatorObserver extends NavigatorObserver { + bool maintainState = false; int pushCount = 0; @override @@ -1366,6 +1424,9 @@ class _MyNavigatorObserver extends NavigatorObserver { if (['nested', '/'].contains(route.settings.name)) { return; } + if (route is PageRoute) { + maintainState = route.maintainState; + } pushCount++; } }