Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 17.2.0

- Adds `overrideOnExit` parameter to `TypedGoRoute` and `TypedRelativeGoRoute` to control whether the `onExit` method of route data classes should be invoked.

## 17.1.0

- Adds `TypedQueryParameter` annotation to override parameter names in `TypedGoRoute` constructors.
Expand Down
41 changes: 37 additions & 4 deletions packages/go_router/lib/src/route_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,20 @@ class _GoRouteParameters {
required this.builder,
required this.pageBuilder,
required this.redirect,
required this.onExit,
this.onExit,
});

final GoRouterWidgetBuilder builder;
final GoRouterPageBuilder pageBuilder;
final GoRouterRedirect redirect;
final ExitCallback onExit;
final ExitCallback? onExit;
}

/// Helper to create [GoRoute] parameters from a factory function and an Expando.
_GoRouteParameters _createGoRouteParameters<T extends _GoRouteDataBase>({
required T Function(GoRouterState) factory,
required Expando<_GoRouteDataBase> expando,
bool overrideOnExit = false,
}) {
Comment thread
hantrungkien marked this conversation as resolved.
T factoryImpl(GoRouterState state) {
final Object? extra = state.extra;
Expand All @@ -123,8 +124,10 @@ _GoRouteParameters _createGoRouteParameters<T extends _GoRouteDataBase>({
factoryImpl(state).buildPage(context, state),
redirect: (BuildContext context, GoRouterState state) =>
factoryImpl(state).redirect(context, state),
onExit: (BuildContext context, GoRouterState state) =>
factoryImpl(state).onExit(context, state),
onExit: overrideOnExit
? (BuildContext context, GoRouterState state) =>
factoryImpl(state).onExit(context, state)
: null,
);
}

Expand Down Expand Up @@ -156,10 +159,12 @@ abstract class GoRouteData extends _GoRouteDataBase {
required T Function(GoRouterState) factory,
GlobalKey<NavigatorState>? parentNavigatorKey,
List<RouteBase> routes = const <RouteBase>[],
bool overrideOnExit = false,
}) {
final _GoRouteParameters params = _createGoRouteParameters<T>(
factory: factory,
expando: _GoRouteDataBase.stateObjectExpando,
overrideOnExit: overrideOnExit,
);

return GoRoute(
Expand Down Expand Up @@ -227,10 +232,12 @@ abstract class RelativeGoRouteData extends _GoRouteDataBase {
required T Function(GoRouterState) factory,
GlobalKey<NavigatorState>? parentNavigatorKey,
List<RouteBase> routes = const <RouteBase>[],
bool overrideOnExit = false,
}) {
final _GoRouteParameters params = _createGoRouteParameters<T>(
factory: factory,
expando: _GoRouteDataBase.stateObjectExpando,
overrideOnExit: overrideOnExit,
);

return GoRoute(
Expand Down Expand Up @@ -483,6 +490,7 @@ class TypedGoRoute<T extends GoRouteData> extends TypedRoute<T> {
this.name,
this.routes = const <TypedRoute<RouteData>>[],
this.caseSensitive = true,
this.overrideOnExit = false,
});

/// The path that corresponds to this route.
Expand Down Expand Up @@ -515,6 +523,18 @@ class TypedGoRoute<T extends GoRouteData> extends TypedRoute<T> {
///
/// Defaults to `true`.
final bool caseSensitive;

/// Whether to override the default behavior of [GoRoute.onExit] to invoke the
/// [GoRouteData.onExit] method of the route data class.
///
/// When `true`, the [GoRouteData.onExit] method will be invoked when
/// the route is removed from GoRouter's route history.
///
/// When `false`, the default behavior is used, which does not invoke
/// the [GoRouteData.onExit] method.
///
/// Defaults to `false`.
final bool overrideOnExit;
Comment thread
hantrungkien marked this conversation as resolved.
Outdated
}

/// A superclass for each typed relative go route descendant
Expand All @@ -526,6 +546,7 @@ class TypedRelativeGoRoute<T extends RelativeGoRouteData>
required this.path,
this.routes = const <TypedRoute<RouteData>>[],
this.caseSensitive = true,
this.overrideOnExit = false,
});

/// The relative path that corresponds to this route.
Expand All @@ -550,6 +571,18 @@ class TypedRelativeGoRoute<T extends RelativeGoRouteData>
///
/// Defaults to `true`.
final bool caseSensitive;

/// Whether to override the default behavior of [GoRoute.onExit] to invoke the
/// [RelativeGoRouteData.onExit] method of the route data class.
///
/// When `true`, the [RelativeGoRouteData.onExit] method will be invoked when
/// the route is removed from GoRouter's route history.
///
/// When `false`, the default behavior is used, which does not invoke
/// the [RelativeGoRouteData.onExit] method.
///
/// Defaults to `false`.
final bool overrideOnExit;
}

/// A superclass for each typed shell route descendant
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 17.1.0
version: 17.2.0
Comment thread
hantrungkien marked this conversation as resolved.
Outdated
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down