Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: don't reset current tab of nested pages on rebuild widget tree. #2081

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 18 additions & 9 deletions auto_route/lib/src/router/controller/routing_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ abstract class RoutingController with ChangeNotifier {

/// Holds track of the list of attached child controllers
List<RoutingController> get childControllers => _childControllers;

RoutingController? get currentChildController => _innerControllerOf(
currentChild?.key
);

final List<AutoRoutePage> _pages = [];

/// The instance of [NavigationHistory] to be used by this router
Expand All @@ -83,6 +88,7 @@ abstract class RoutingController with ChangeNotifier {
_childControllers.remove(childController);
}

/// Returns the last child controller with specified [key].
RoutingController? _innerControllerOf(Key? key) =>
_childControllers.lastWhereOrNull(
(c) => c.key == key,
Expand Down Expand Up @@ -747,16 +753,19 @@ class TabsRouter extends RoutingController {
/// after match validation and deciding initial index
void setupRoutes(List<PageRouteInfo>? routes) {
final routesToPush = _resolveRoutes(routes);
RouteMatch? preMatchedRoute;
if (_routeData.hasPendingChildren) {
final preMatchedRoute = _routeData.pendingChildren.last;
final correspondingRouteIndex = routesToPush.indexWhere(
(r) => r.key == preMatchedRoute.key,
);
if (correspondingRouteIndex != -1) {
routesToPush[correspondingRouteIndex] = preMatchedRoute;
_previousIndex = _activeIndex;
_activeIndex = correspondingRouteIndex;
}
preMatchedRoute = _routeData.pendingChildren.last;
} else {
preMatchedRoute = _routeData.route;
}
final correspondingRouteIndex = routesToPush.indexWhere(
(r) => r.key == preMatchedRoute!.key,
);
if (correspondingRouteIndex != -1) {
routesToPush[correspondingRouteIndex] = preMatchedRoute;
_previousIndex = _activeIndex;
_activeIndex = correspondingRouteIndex;
}

if (routesToPush.isNotEmpty) {
Expand Down
3 changes: 2 additions & 1 deletion auto_route/lib/src/router/widgets/auto_tabs_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ abstract class AutoTabsRouterState<T extends AutoTabsRouter> extends State<T> {
parent: _parentController,
key: parentRoute.key,
homeIndex: widget.homeIndex,
routeData: parentRoute,
routeData: _parentController.currentChildController?.current
?? parentRoute,
preload: onPreload,
routeCollection: _parentController.routeCollection.subCollectionOf(
parentRoute.name,
Expand Down