flutter 在navigatorBuilder函数中实现导航控件时,无法使用Go Router导航

x4shl7ld  于 2023-01-21  发布在  Flutter
关注(0)|答案(1)|浏览(125)

描述

我正在尝试使用go_router plugin通过导航菜单在屏幕之间导航。当我单击菜单中的项目时,没有任何React,但如果我更改URL,屏幕确实会更改。
Video shows the problem

期待

每次我来回导航时,URL和屏幕都会发生变化

我的代码

应用程序_路由器.dart

class AppRouter {
  AppRouter(this._appBloc);

  final AppBloc _appBloc;

  GoRouter get router => GoRouter(
        routes: pages.values.toList(growable: false),
        errorBuilder: (context, state) => ErrorPage(
          key: state.pageKey,
        ),
        refreshListenable: GoRouterRefreshStream(_appBloc.stream),
        navigatorBuilder: _buildRouterView,
        redirect: _redirect,
      );

  String? _redirect(GoRouterState state) {
    final loggedIn = _appBloc.state.status == AppStatus.authenticated;

    final name = state.subloc;

    final loggingIn = name == '/login' || name == '/';

    if (!loggedIn) return loggingIn ? null : '/login';

    if (loggingIn) return '/app';

    return null;
  }

  static Map<String, GoRoute> pages = {
    route_names.onboard: GoRoute(
      name: route_names.onboard,
      path: routes[route_names.onboard]!,
      pageBuilder: (context, state) => OnboardPage.page(key: state.pageKey),
      routes: [
        GoRoute(
          path: route_names.login.subRoutePath,
          name: route_names.login,
          pageBuilder: (context, state) => LoginPage.page(key: state.pageKey),
        ),
        GoRoute(
          path: route_names.signUp.subRoutePath,
          name: route_names.signUp,
          pageBuilder: (context, state) => LoginPage.page(key: state.pageKey),
        ),
      ],
    ),
    'app': GoRoute(
        path: '/app',

        // All /app pages get the main scaffold
        builder: (context, state) {
          return Text("App Main");
        },
        routes: [
          ExplorePage.route,
          PlanPage.route,
          AccountPage.route,
        ]),
  };

  Widget _buildRouterView(BuildContext context, GoRouterState state, Widget child) {
    return Builder(
      builder: (context) => BlocBuilder<AppBloc, AppState>(builder: (context, appState) {
        if (appState.status == AppStatus.unauthenticated) {
          return child;
        }
        return HomePageSkeleton(
          child: child,
        );
      }),
    );
  }
}

app.dart

class AppView extends StatelessWidget {
  // ignore: prefer_const_constructors_in_immutables
  AppView({super.key, required AppBloc appBloc}) {
    _appBloc = appBloc;
    _appRouter = AppRouter(_appBloc);
  }
  late final AppBloc _appBloc;
  late final AppRouter _appRouter;

  @override
  Widget build(BuildContext context) {
    return BlocListener<AppBloc, AppState>(
      listener: (context, state) {
        if (state == const AppState.unauthenticated()) {
          _appRouter.router.goNamed(route_names.login);
        }
      },
      child: MaterialApp.router(

        supportedLocales: AppLocalizations.supportedLocales,
        routeInformationParser: _appRouter.router.routeInformationParser,
        routeInformationProvider: _appRouter.router.routeInformationProvider,
        routerDelegate: _appRouter.router.routerDelegate,
      ),
    );
  }
}

HomePageSkeleton.class
一个二个一个一个

euoag5mw

euoag5mw1#

我将AppRouter类更改为:

class AppRouter {
  AppRouter(AppBloc appBloc)
      : _router = GoRouter(
          routes: getPages().values.toList(growable: false),
          errorBuilder: (context, state) => ErrorPage(
            key: state.pageKey,
          ),
          refreshListenable: GoRouterRefreshStream(appBloc.stream),
          navigatorBuilder: _buildRouterView,
          redirect: (GoRouterState state) {
            _redirect(state, appBloc);
          },
        ),
        _appBloc = appBloc;

  final AppBloc _appBloc;

  final GoRouter _router;

  GoRouter get router {
    return _router;
  }

......而且成功了

相关问题