dart 使用go router时,Flutter导航将转到父路由而不是子路由

9q78igpj  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(193)

我是一个全新的Flutter,我最近添加了去路由器到我的应用程序,以解决一些问题的导航。
现在,我正面临着这个问题,我找不到任何答案。
在下面的代码中,homegoal的父级。
我在一个页面,我需要直接进入目标,但当我这样做时,
第一个月
它去父母(这是家)。我不知道我做错了什么
这是我的代码:

GoRouter getRoutes() { 
    return GoRouter(initialLocation: '/', 
    observers: [ DoubleTapExitAppObserver() ], 
    routes: <RouteBase>[ 
    GoRoute(
    path: '/home', 
    name: 'home', 
    builder: (BuildContext context, GoRouterState state) {
        return HomeScreen();
      },
      routes: [
        GoRoute(
          name: 'home_with_count',
          path: ':gCount',
          builder: (BuildContext context, GoRouterState state) {

            int? goalCount = int.tryParse(
                state.pathParameters.containsKey("gCount")
                    ? state.pathParameters["gCount"] ?? ""
                    : "");
            return HomeScreen(
             gCount: gCount,
            );
          },
        ),
        GoRoute(
          name: 'home_profile_loaded',
          path: ':profileLoaded',
          builder: (BuildContext context, GoRouterState state) {

            bool profileLoaded = bool.tryParse(
                    state.pathParameters.containsKey("profileLoaded")
                        ? state.pathParameters["profileLoaded"] ?? ""
                        : "") ??
                false;
            return HomeScreen(
             profileLoaded: profileLoaded,
            );
          },
        ),

        GoRoute(
            name: 'goal',
            path: 'goal',
            builder: (BuildContext context, GoRouterState state) {
              return const GoalsListAndTrack();
            },
            routes: [
              GoRoute(
                name: 'allocate',
                path: 'allocate',
                builder: (BuildContext context, GoRouterState state) {
                  Goal goal = state.extra as Goal;
                  return Allocate(goal: goal);
                },
              ),
              GoRoute(
                name: 'goal_details',
                path: 'goal_details',
                builder: (BuildContext context, GoRouterState state) {
                  Goal goal = state.extra as Goal;
                  return GoalDetailsCard(goal: goal);
                },
              ),
            ]),

      ]),
  GoRoute(
    path: '/',
    // name: 'splash',
    builder: (BuildContext context, GoRouterState state) {
      return const SplashScreen();
    },
  ),
  GoRoute(
    name: 'login',
    path: '/login',
    builder: (BuildContext context, GoRouterState state) {
      return const LoginScreen();
    },
  ),
]);
}

字符串
此外,如果对上述代码有任何建议,将有所帮助。
先谢谢你了!

qv7cva1a

qv7cva1a1#

有很多方法可以在屏幕之间导航。
方法1:使用Navigator.push()转到其他页面,使用Navigator.pop()返回第一个路由。

Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => const SecondRoute()),
  );

字符串
Reade more
方法2:使用GetX在应用程序中的页面之间进行路由非常简单和专业。

Get.to(NextScreen());


Reade more

相关问题