我使用导航包auto_route:^7.8.0。
如何使用它来显示初始登录页面,这取决于用户是否登录到firebase?
main.dart
final _appRouter = AppRouter();
@override
Widget build(BuildContext context) {
return GlobalBlocInjector(
child: MaterialApp.router(
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
routerConfig: _appRouter.config(
reevaluateListenable: ReevaluateListenable.stream(FirebaseAuth.instance.authStateChanges()),
navigatorObservers: () => [
TalkerRouteObserver(GetIt.I<Talker>()),
],
),
locale: const Locale('ru'),
supportedLocales: S.delegate.supportedLocales,
theme: const DarkThemeBuilder().build(),
scaffoldMessengerKey: GetIt.instance<SnackBarService>(),
),
);
}
}
app_router.dart
import 'package:auto_route/auto_route.dart';
import '../../presentation/presentation.dart';
import '../app.dart';
part 'router.gr.dart';
@AutoRouterConfig()
class AppRouter extends _$AppRouter {
@override
List<AutoRoute> get routes => [
AutoRoute(page: SignInRoute.page, initial: true,),
AutoRoute(page: SignUpRoute.page),
AutoRoute(page: MainRoute.page),
AutoRoute(page: ResetPasswordRoute.page),
AutoRoute(page: ChangePasswordRoute.page),
AutoRoute(page: ReVerificationRoute.page),
];
}
我试图按照pub.dev上列出的文档进行操作,但不幸的是没有任何结果
@AutoRouterConfig()
class AppRouter extends $AppRouter implements AutoRouteGuard {
@override
void onNavigation(NavigationResolver resolver, StackRouter router) {
if(isAuthenticated || resolver.route.name == LoginRoute.name){
// we continue navigation
resolver.next();
}else{
// else we navigate to the Login page so we get authenticateed
// tip: use resolver.redirect to have the redirected route
// automatically removed from the stack when the resolver is completed
resolver.redirect(LoginRoute(onResult:(didLogin)=> resolver.next(didLogin)))
}
}
// ..routes[]
}
1条答案
按热度按时间lymnna711#
您可以检查
这更容易实现。