flutter 如何从ShellRoute走下一条路线?

pcww981p  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(136)

我正在使用ShellRoute来控制BottomNavigationBar。它工作正常。但是当我尝试context.go时,我得到的消息是没有找到给定的路径。

static final _rootNavigatorKey = GlobalKey<NavigatorState>();
  static final _shellNavigatorKey = GlobalKey<NavigatorState>();

  static final GoRouter router = GoRouter(
    initialLocation: AppRoutes.home,
    debugLogDiagnostics: true,
    navigatorKey: _rootNavigatorKey,
    routes: [
      ShellRoute(
        navigatorKey: _shellNavigatorKey,
        builder: (context, state, child) {
          return BlocProvider(
            create: (context) => NavigationCubit(),
            child: MainScreen(child: child),
          );
        },
        routes: [
          GoRoute(
            parentNavigatorKey: _shellNavigatorKey,
            path: AppRoutes.home,
            pageBuilder: (context, state) => const NoTransitionPage(
              child: HomeScreen(),
            ),
            routes: [
              GoRoute(
                parentNavigatorKey: _shellNavigatorKey,
                path: AppRoutes.homeDetails,
                builder: (context, state) => const HomeDetailsScreen(),
              ),
            ],
          ),
          GoRoute(
            parentNavigatorKey: _shellNavigatorKey,
            path: AppRoutes.profile,
            pageBuilder: (context, state) => const NoTransitionPage(
              child: ProfileScreen(),
            ),
            routes: [
              GoRoute(
                parentNavigatorKey: _shellNavigatorKey,
                path: AppRoutes.profileDetails,
                builder: (context, state) => const ProfileDetailsScreen(),
              ),
            ],
          ),
          GoRoute(
            parentNavigatorKey: _shellNavigatorKey,
            path: AppRoutes.settings,
            pageBuilder: (context, state) => const NoTransitionPage(
              child: SettingsScreen(),
            ),
          ),
        ],
      ),
    ],
    errorBuilder: (context, state) => const ErrorScreen(),
  );

字符串
当我尝试从AppRoutes.home转到AppRoutes.homeDatails时:

context.go(AppRoutes.homeDetails),


我得到了结果:

[GoRouter] No initial matches: details


我尝试添加parentNavigatorKey,但它没有帮助. GoRouter.of(context).go(AppRoutes.homeDetails)也不工作.

bqjvbblv

bqjvbblv1#

我遇到了同样的问题,这是对我起作用的:
如果您在AppRouter中保留所有路径,以便从应用程序的不同屏幕访问它们,请确保它们是完整路径,如:

class AppRouter {
  static const home = '/home';
  static const homeDetails = '/home/details';

字符串
但是,在GoRouter对象中,您应该使用短路径,而不使用“/”表示子路由,如:

GoRoute(
  parentNavigatorKey: _shellNavigatorKey,
  path: 'details',
  builder: (context, state) => const HomeDetailsScreen(),
),


这样,您就可以使用context.go(AppRoutes.homeDetails),并且它应该可以按预期工作!

相关问题