'Why Flutter GoRouter ignores pageKey

Regarding the documentation if Flutter identifies the same key it will update the url and keep the page (Widget). In the code below pageKey is a static const value set in both Containers. When navigating between those two routes (/home & /home/login) Flutter ignores it's the same widget and recreates it.

class ProjectXApp extends StatelessWidget {

  ProjectXApp({Key? key}) : super(key: key);

  static const pageKey = ValueKey("Home");

  final _router = GoRouter(
    routes: [
      GoRoute(
        path: '/',
        redirect: (_) => Routes.routeHome,
      ),
      GoRoute(
        path: '/home',
        builder: (context, state) {
          return Container(key: pageKey);
        }
      ),
      GoRoute(
          path: "/home/login",
          builder: (context, state) {
            return Container(key: pageKey);
          }
      )
    ],
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
        routeInformationParser: _router.routeInformationParser,
        routerDelegate: _router.routerDelegate
    );
  }
}

Why is it happening? Is there a way to achieve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source