dart 如何在Flutter - AutoRoute中从子路由访问TabsRouter

izkcnapc  于 2023-03-10  发布在  Flutter
关注(0)|答案(1)|浏览(121)

我试图从它的子路由器得到TabsRouter,但它返回null(所以我猜它找不到任何相关的路由器)。尝试了一些东西,阅读了几次文档。它要么不工作,因为它应该或我不明白。

  • 项目是唯一的Android和IOS,其他平台不支持.
  • 使用的 Package :auto_route
AutoRoute(
      path: NavRoutes.authPage,
      page: AuthPage,
      children: [
        AutoRoute(
          path: NavRoutes.loginPage,
          page: LoginPage,
        ),
        AutoRoute(
          path: NavRoutes.signupPage,
          page: SignUpPage,
        ),
      ],
    )

class AuthView extends StatelessWidget {
  const AuthView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final cubit = BlocProvider.of<AuthCubit>(context);
    return Scaffold(
      body: SafeArea(
        child: AutoTabsRouter(
          routes: [
            const LoginPageRoute(),
            SignUpPageRoute(
              key: UniqueKey(),
              onGooglePressed: cubit.signInGoogle,
            ),
          ],
          builder: (context, child, animation) {
            return FadeTransition(
              opacity: animation,
              child: child,
            );
          },
        ),
      ),
    );
  }
}

// Here I create `AutoTabsRouter` inside AuthView, and try to get it inside any of its child like:

context.innerRouterOf<TabsRouter>(AuthPageRoute.name)?.setActiveIndex(1);
context.innerRouterOf<TabsRouter>(LoginPageRoute.name)?.setActiveIndex(1);
AutoTabsRouter.of(context).setActiveIndex(1);

//tried a few more using innerNavKeys but it also failed.
4dc9hkyq

4dc9hkyq1#

你试着导航到目标选项卡了吗?

context.navigateTo(TargetTabRoute());

相关问题