如何使用带有 auto_route 的嵌套路由在 Flutter 中的屏幕之间导航

Seb*_*ebb 6 routes dart flutter

我使用 auto_routes 包设置了嵌套路由。它看起来像这样:

  replaceInRouteName: 'Page,Route',
  routes: [
    AutoRoute(
      path: '/',
      page: HomePage,
      children: [
        AutoRoute(
          path: 'dashboard',
          name: 'DashboardRouter',
          page: EmptyRouterPage,
          children: [
            AutoRoute(
              path: '',
              page: DashboardPage,
            ),
            AutoRoute(
              path: ':regional',
              page: TopPlayersPage,
            ),
            AutoRoute(page: EditProfilePage),
            // AutoRoute(page: ProfilePage),
          ],
        ),
        AutoRoute(
          path: 'profile',
          name: 'ProfileRouter',
          page: ProfilePage,
        ),
        AutoRoute(
          path: 'search',
          name: 'SearchRouter',
          page: EmptyRouterPage,
          children: [
            AutoRoute(
              path: '',
              page: SearchOverviewPage,
            ),
            AutoRoute(
              path: ':searchType',
              page: SearchPage,
            )
          ],
        ),
        AutoRoute(
          path: 'add_post',
          name: 'AddPostRouter',
          page: AddPostPage,
        ),
        AutoRoute(
          path: 'notifications',
          name: 'NotificationsRouter',
          page: NotificationsPage,
        ),
        AutoRoute(
          path: 'edit_profile',
          name: 'EditProfileRouter',
          page: EditProfilePage,
        )
      ],
    ),
  ],
...
);
)
Run Code Online (Sandbox Code Playgroud)

上面的大部分部分都在 AutoTabsScaffold 中使用,它与 auto_routes 一起提供,以创建底部导航栏。

return AutoTabsScaffold(
      backgroundColor: LIGHT_MODE_WHITE,
      routes: const [
        DashboardRouter(),
        SearchRouter(),
        AddPostRouter(),
        NotificationsRouter(),
        EditProfileRouter(),
      ],
Run Code Online (Sandbox Code Playgroud)

我现在想要从仪表板页面中包含的小部件导航到我的个人资料页面,该页面未在底部导航栏中使用。我尝试使用此代码执行此操作,但没有任何反应(参数“playerId”在我在 ProfileRouter 中使用的类中被标记为必需):

@override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        context.navigateTo(
          ProfileRouter(
            playerId: 'Test',
          ),
        );
      },
... 
}
Run Code Online (Sandbox Code Playgroud)

当我将 ProfileRouter 替换为底部导航栏中使用的路由器时,它工作正常。知道如何解决这个问题吗?当我将 ProfileRouter 集成到路由设置的仪表板部分时,我可以使用 router.push() 访问它,但由于我也想从代码的其他部分访问此页面,所以这不是可行的方法我认为。

谢谢你!

Seb*_*ebb 4

发现问题所在了。路线ProfilePage必须提升一个级别。