diff --git a/packages/go_router/lib/src/information_provider.dart b/packages/go_router/lib/src/information_provider.dart index dc979193b325..c969d21471fa 100644 --- a/packages/go_router/lib/src/information_provider.dart +++ b/packages/go_router/lib/src/information_provider.dart @@ -33,6 +33,9 @@ enum NavigatingType { /// Restore the current match list with /// [RouteInformationState.baseRouteMatchList]. restore, + + /// Pop Current route. + pop, } /// The data class to be stored in [RouteInformation.state] to be used by @@ -48,7 +51,9 @@ class RouteInformationState { this.completer, this.baseRouteMatchList, required this.type, - }) : assert((type == NavigatingType.go || type == NavigatingType.restore) == + }) : assert((type == NavigatingType.go || + type == NavigatingType.restore || + type == NavigatingType.pop) == (completer == null)), assert((type != NavigatingType.go) == (baseRouteMatchList != null)); @@ -217,6 +222,17 @@ class GoRouteInformationProvider extends RouteInformationProvider return completer.future; } + /// Save the pop state to value when remove top-most route. + void popSave(String location, {required RouteMatchList base}) { + _value = RouteInformation( + uri: Uri.parse(location), + state: RouteInformationState( + baseRouteMatchList: base, + type: NavigatingType.pop, + ), + ); + } + RouteInformation _valueInEngine; void _platformReportsNewRouteInformation(RouteInformation routeInformation) { diff --git a/packages/go_router/lib/src/parser.dart b/packages/go_router/lib/src/parser.dart index b4115a1fca19..7e207d68a047 100644 --- a/packages/go_router/lib/src/parser.dart +++ b/packages/go_router/lib/src/parser.dart @@ -217,6 +217,9 @@ class GoRouteInformationParser extends RouteInformationParser { return baseRouteMatchList!.uri.toString() != newMatchList.uri.toString() ? newMatchList : baseRouteMatchList; + case NavigatingType.pop: + // Will not do anything. + return baseRouteMatchList!; } } diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index dc6d88057eb0..4211145b1d27 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -487,7 +487,12 @@ class GoRouter implements RouterConfig { log('popping ${routerDelegate.currentConfiguration.uri}'); return true; }()); + routerDelegate.pop(result); + routeInformationProvider.popSave( + routerDelegate.currentConfiguration.uri.toString(), + base: routerDelegate.currentConfiguration, + ); } /// Refresh the route. @@ -556,6 +561,7 @@ class GoRouter implements RouterConfig { /// A routing config that is never going to change. class _ConstantRoutingConfig extends ValueListenable { const _ConstantRoutingConfig(this.value); + @override void addListener(VoidCallback listener) { // Intentionally empty because listener will never be called.