flutter go router shellRoute builder函数未在context.push()上调用

cmssoen2  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(140)

使用context.push('/profile/check');时,builder函数未被调用,因此Text("Check")显示在AppScaffold内部。

late final GoRouter router = GoRouter(
    initialLocation: '/',
    refreshListenable: appService,
    navigatorKey: _rootNavigatorKey,
    routes: <RouteBase>[
      ShellRoute(
        navigatorKey: _rootShellNavigatorKey,
        builder: (BuildContext context, GoRouterState state, child) => AppScaffold(
          child: child,
        ),
        routes: [
          GoRoute(
            parentNavigatorKey: _rootShellNavigatorKey,
            path: '/',
            builder: (BuildContext context, GoRouterState state) {
              return HomeScreen();
            },
          ),
          GoRoute(
            parentNavigatorKey: _rootShellNavigatorKey,
            path: '/profile',
            builder: (BuildContext context, GoRouterState state) {
              return ProfilePageView();
            },
            routes: [
              ShellRoute(
                builder: (context, state, child) {
                  print('builder function not being called');
                  return Text("hello world");
                },
                routes: [
                  GoRoute(
                    path: 'check',
                    builder: (BuildContext context, GoRouterState state) {
                      return Center(
                        child: Text("Check"),
                      );
                    },
                  ),
                  GoRoute(
                    path: 'test',
                    builder: (BuildContext context, GoRouterState state) {
                      return Center(
                        child: Text("Test"),
                      );
                    },
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    ],
  );
0lvr5msh

0lvr5msh1#

如果遇到同样的问题,使用GoRouter.of(context).go('...');,一切都应该正常

相关问题