From ddc6c3432c0c75c6868a1b6b6f010a91e4eef11b Mon Sep 17 00:00:00 2001 From: tbm98 <52562340+tbm98@users.noreply.github.com> Date: Sat, 19 Jun 2021 02:24:07 +0700 Subject: [PATCH] Reland: Add optional param to useRootNavigator when showSearch (#84872) --- packages/flutter/lib/src/material/search.dart | 10 ++- .../flutter/test/material/search_test.dart | 73 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/search.dart b/packages/flutter/lib/src/material/search.dart index f56eeb93c2..b0acc787b6 100644 --- a/packages/flutter/lib/src/material/search.dart +++ b/packages/flutter/lib/src/material/search.dart @@ -35,6 +35,12 @@ import 'theme.dart'; /// call. Call [SearchDelegate.close] before re-using the same delegate instance /// for another [showSearch] call. /// +/// The `useRootNavigator` argument is used to determine whether to push the +/// search page to the [Navigator] furthest from or nearest to the given +/// `context`. By default, `useRootNavigator` is `false` and the search page +/// route created by this method is pushed to the nearest navigator to the +/// given `context`. It can not be `null`. +/// /// 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]. @@ -54,12 +60,14 @@ Future showSearch({ required BuildContext context, required SearchDelegate delegate, String? query = '', + bool useRootNavigator = false, }) { assert(delegate != null); assert(context != null); + assert(useRootNavigator != null); delegate.query = query ?? delegate.query; delegate._currentBody = _SearchBody.suggestions; - return Navigator.of(context).push(_SearchPageRoute( + return Navigator.of(context, rootNavigator: useRootNavigator).push(_SearchPageRoute( delegate: delegate, )); } diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index 9eaf2c8a50..f3e38a50c8 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -841,6 +841,66 @@ void main() { expect(find.text('Suggestions'), findsNothing); expect(selectedResults, ['Result']); }); + + testWidgets('showSearch with useRootNavigator', (WidgetTester tester) async { + final _MyNavigatorObserver rootObserver = _MyNavigatorObserver(); + final _MyNavigatorObserver localObserver = _MyNavigatorObserver(); + + final _TestEmptySearchDelegate delegate = _TestEmptySearchDelegate( + result: 'Result', + suggestions: 'Suggestions', + ); + + await tester.pumpWidget(MaterialApp( + navigatorObservers: [rootObserver], + home: Navigator( + observers: [localObserver], + onGenerateRoute: (RouteSettings settings) { + if (settings.name == 'nested') { + return MaterialPageRoute( + builder: (BuildContext context) => Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + TextButton( + onPressed: () async { + await showSearch(context: context, delegate: delegate, useRootNavigator: true); + }, + child: const Text('showSearchRootNavigator')), + TextButton( + onPressed: () async { + await showSearch(context: context, delegate: delegate); + }, + child: const Text('showSearchLocalNavigator')), + ], + ), + settings: settings, + ); + } + throw UnimplementedError(); + }, + initialRoute: 'nested', + ), + )); + + expect(rootObserver.pushCount, 0); + expect(localObserver.pushCount, 0); + + // showSearch normal and back + await tester.tap(find.text('showSearchLocalNavigator')); + await tester.pumpAndSettle(); + await tester.tap(find.byTooltip('Close')); + await tester.pumpAndSettle(); + expect(rootObserver.pushCount, 0); + expect(localObserver.pushCount, 1); + + // showSearch with rootNavigator + await tester.tap(find.text('showSearchRootNavigator')); + await tester.pumpAndSettle(); + await tester.tap(find.byTooltip('Close')); + await tester.pumpAndSettle(); + expect(rootObserver.pushCount, 1); + expect(localObserver.pushCount, 1); + }); } class TestHomePage extends StatelessWidget { @@ -1026,3 +1086,16 @@ class _TestEmptySearchDelegate extends SearchDelegate { ); } } + +class _MyNavigatorObserver extends NavigatorObserver { + int pushCount = 0; + + @override + void didPush(Route route, Route? previousRoute) { + // don't count the root route + if (['nested', '/'].contains(route.settings.name)) { + return; + } + pushCount++; + } +}