From a6df1ffd77ab06a32f93289197773a7890ccfe61 Mon Sep 17 00:00:00 2001 From: linpengteng Date: Fri, 22 Nov 2024 10:16:53 +0800 Subject: [PATCH 1/2] fix(lib/get_navigation): support preventDuplicateHandlingMode in copyWith (GetPage) --- lib/get_navigation/src/routes/get_route.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/get_navigation/src/routes/get_route.dart b/lib/get_navigation/src/routes/get_route.dart index 3968f7511..e3c95eec0 100644 --- a/lib/get_navigation/src/routes/get_route.dart +++ b/lib/get_navigation/src/routes/get_route.dart @@ -133,6 +133,7 @@ class GetPage extends Page { bool? canPop, PopInvokedWithResultCallback? onPopInvoked, String? restorationId, + PreventDuplicateHandlingMode? preventDuplicateHandlingMode, }) { return GetPage( key: key ?? this.key, @@ -168,7 +169,9 @@ class GetPage extends Page { inheritParentPath: inheritParentPath ?? this.inheritParentPath, canPop: canPop ?? this.canPop, onPopInvoked: onPopInvoked ?? this.onPopInvoked, - restorationId: restorationId ?? restorationId, + restorationId: restorationId ?? this.restorationId, + preventDuplicateHandlingMode: + preventDuplicateHandlingMode ?? this.preventDuplicateHandlingMode, ); } From 208f01690e10a21422971085c3b294f3c60b8c2f Mon Sep 17 00:00:00 2001 From: linpengteng Date: Fri, 22 Nov 2024 12:57:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20using=20preventDuplicates=20option?= =?UTF-8?q?s=20(Get.off=E3=80=81Get.toNamed=20...)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. using preventDuplicates option of Get Navigation - to、off、offNamed、offUntil、backAndtoNamed、 - toNamed、offNamedUntil、toNamedAndOffUntil 2. merge preventDuplicates option in _configureRouterDecoder API 3. execute _activePages.add(res) when route.preventDuplicates == false --- .../src/extension_navigation.dart | 9 ++- .../src/routes/get_navigation_interface.dart | 11 ++- .../src/routes/get_router_delegate.dart | 71 +++++++++++++++---- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/lib/get_navigation/src/extension_navigation.dart b/lib/get_navigation/src/extension_navigation.dart index 13718e4ac..ea766e69e 100644 --- a/lib/get_navigation/src/extension_navigation.dart +++ b/lib/get_navigation/src/extension_navigation.dart @@ -629,6 +629,7 @@ extension GetNavigationExt on GetInterface { dynamic arguments, String? id, Map? parameters, + bool preventDuplicates = true, }) { // if (preventDuplicates && page == currentRoute) { // return null; @@ -642,7 +643,7 @@ extension GetNavigationExt on GetInterface { page, arguments: arguments, id: id, - // preventDuplicates: preventDuplicates, + preventDuplicates: preventDuplicates, parameters: parameters, ); } @@ -690,6 +691,7 @@ extension GetNavigationExt on GetInterface { String? id, dynamic arguments, Map? parameters, + bool preventDuplicates = true, }) { if (parameters != null) { final uri = Uri(path: page, queryParameters: parameters); @@ -702,6 +704,7 @@ extension GetNavigationExt on GetInterface { id: id, arguments: arguments, parameters: parameters, + preventDuplicates: preventDuplicates, ); } @@ -722,6 +725,7 @@ extension GetNavigationExt on GetInterface { String? id, dynamic result, Map? parameters, + bool preventDuplicates = true, }) { if (parameters != null) { final uri = Uri(path: page, queryParameters: parameters); @@ -731,6 +735,7 @@ extension GetNavigationExt on GetInterface { page, arguments: arguments, result: result, + preventDuplicates: preventDuplicates, ); } @@ -1003,11 +1008,13 @@ extension GetNavigationExt on GetInterface { bool Function(GetPage) predicate, [ Object? arguments, String? id, + bool preventDuplicates = true, ]) { return searchDelegate(id).offUntil( page, predicate, arguments, + preventDuplicates, ); } diff --git a/lib/get_navigation/src/routes/get_navigation_interface.dart b/lib/get_navigation/src/routes/get_navigation_interface.dart index 3e2290801..05eb52ce3 100644 --- a/lib/get_navigation/src/routes/get_navigation_interface.dart +++ b/lib/get_navigation/src/routes/get_navigation_interface.dart @@ -117,6 +117,7 @@ mixin IGetNavigation { dynamic arguments, String? id, Map? parameters, + bool preventDuplicates = true, }); Future? offAllNamed( @@ -133,25 +134,33 @@ mixin IGetNavigation { dynamic arguments, String? id, Map? parameters, + bool preventDuplicates = true, }); Future toNamedAndOffUntil( String page, bool Function(GetPage) predicate, [ Object? data, + bool preventDuplicates = true, ]); Future offUntil( Widget Function() page, bool Function(GetPage) predicate, [ Object? arguments, + bool preventDuplicates = true, ]); void removeRoute(String name); void back([T? result]); - Future backAndtoNamed(String page, {T? result, Object? arguments}); + Future backAndtoNamed( + String page, { + T? result, + Object? arguments, + bool preventDuplicates = true, + }); void backUntil(bool Function(GetPage) predicate); diff --git a/lib/get_navigation/src/routes/get_router_delegate.dart b/lib/get_navigation/src/routes/get_router_delegate.dart index d05023454..d00011b50 100644 --- a/lib/get_navigation/src/routes/get_router_delegate.dart +++ b/lib/get_navigation/src/routes/get_router_delegate.dart @@ -355,7 +355,10 @@ class GetDelegate extends RouterDelegate Map? parameters, }) async { final args = _buildPageSettings(page, arguments); - final route = _getRouteDecoder(args); + final route = _getRouteDecoder( + args, + preventDuplicates: preventDuplicates, + ); if (route != null) { return _push(route); } else { @@ -407,7 +410,10 @@ class GetDelegate extends RouterDelegate _routeTree.addRoute(getPage); final args = _buildPageSettings(routeName, arguments); - final route = _getRouteDecoder(args); + final route = _getRouteDecoder( + args, + preventDuplicates: preventDuplicates, + ); final result = await _push( route!, rebuildStack: rebuildStack, @@ -521,9 +527,13 @@ class GetDelegate extends RouterDelegate dynamic arguments, String? id, Map? parameters, + bool preventDuplicates = true, }) async { final args = _buildPageSettings(page, arguments); - final route = _getRouteDecoder(args); + final route = _getRouteDecoder( + args, + preventDuplicates: preventDuplicates, + ); if (route == null) return null; final newPredicate = predicate ?? (route) => false; @@ -541,9 +551,13 @@ class GetDelegate extends RouterDelegate dynamic arguments, String? id, Map? parameters, + bool preventDuplicates = true, }) async { final args = _buildPageSettings(page, arguments); - final route = _getRouteDecoder(args); + final route = _getRouteDecoder( + args, + preventDuplicates: preventDuplicates, + ); if (route == null) return null; _popWithResult(); return _push(route); @@ -554,10 +568,14 @@ class GetDelegate extends RouterDelegate String page, bool Function(GetPage) predicate, [ Object? data, + bool preventDuplicates = true, ]) async { final arguments = _buildPageSettings(page, data); - final route = _getRouteDecoder(arguments); + final route = _getRouteDecoder( + arguments, + preventDuplicates: preventDuplicates, + ); if (route == null) return null; @@ -573,12 +591,17 @@ class GetDelegate extends RouterDelegate Widget Function() page, bool Function(GetPage) predicate, [ Object? arguments, + bool preventDuplicates = true, ]) async { while (_activePages.isNotEmpty && !predicate(_activePages.last.route!)) { _popWithResult(); } - return to(page, arguments: arguments); + return to( + page, + arguments: arguments, + preventDuplicates: preventDuplicates, + ); } @override @@ -602,10 +625,17 @@ class GetDelegate extends RouterDelegate } @override - Future backAndtoNamed(String page, - {T? result, Object? arguments}) async { + Future backAndtoNamed( + String page, { + T? result, + Object? arguments, + bool preventDuplicates = true, + }) async { final args = _buildPageSettings(page, arguments); - final route = _getRouteDecoder(args); + final route = _getRouteDecoder( + args, + preventDuplicates: preventDuplicates, + ); if (route == null) return null; _popWithResult(result); return _push(route); @@ -692,7 +722,10 @@ class GetDelegate extends RouterDelegate } @protected - RouteDecoder? _getRouteDecoder(PageSettings arguments) { + RouteDecoder? _getRouteDecoder( + PageSettings arguments, { + bool? preventDuplicates, + }) { var page = arguments.uri.path; final parameters = arguments.params; if (parameters.isNotEmpty) { @@ -704,12 +737,19 @@ class GetDelegate extends RouterDelegate final route = decoder.route; if (route == null) return null; - return _configureRouterDecoder(decoder, arguments); + return _configureRouterDecoder( + decoder, + arguments, + preventDuplicates: preventDuplicates, + ); } @protected RouteDecoder _configureRouterDecoder( - RouteDecoder decoder, PageSettings arguments) { + RouteDecoder decoder, + PageSettings arguments, { + bool? preventDuplicates, + }) { final parameters = arguments.params.isEmpty ? arguments.query : arguments.params; arguments.params.addAll(arguments.query); @@ -722,6 +762,7 @@ class GetDelegate extends RouterDelegate arguments: arguments, parameters: parameters, key: ValueKey(arguments.name), + preventDuplicates: preventDuplicates, ); return decoder; @@ -737,11 +778,13 @@ class GetDelegate extends RouterDelegate res.route?.preventDuplicateHandlingMode ?? PreventDuplicateHandlingMode.reorderRoutes; + final preventDuplicates = res.route?.preventDuplicates ?? true; + final onStackPage = _activePages .firstWhereOrNull((element) => element.route?.key == res.route?.key); - /// There are no duplicate routes in the stack - if (onStackPage == null) { + /// There are no duplicate routes in the stack or route.preventDuplicates == false + if (onStackPage == null || preventDuplicates == false) { _activePages.add(res); } else { /// There are duplicate routes, reorder