app_routes.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'package:chicken_farm/stores/auth_store.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:go_router/go_router.dart';
  5. import '../pages/account/login.dart';
  6. import '../pages/home/home.dart';
  7. class AppRoutePaths {
  8. static const String splash = '/';
  9. static const String login = '/login';
  10. static const String home = '/home';
  11. }
  12. class AppRoutes {
  13. static final List<GoRoute> routes = [
  14. GoRoute(
  15. path: AppRoutePaths.splash,
  16. builder: (context, state) => const SplashScreen(),
  17. ),
  18. GoRoute(
  19. path: AppRoutePaths.login,
  20. name: AppRoutePaths.login,
  21. builder: (context, state) => const LoginPage(),
  22. ),
  23. GoRoute(
  24. path: AppRoutePaths.home,
  25. name: AppRoutePaths.home,
  26. builder: (context, state) => const HomePage(),
  27. ),
  28. ];
  29. }
  30. // 启动屏
  31. class SplashScreen extends ConsumerWidget {
  32. const SplashScreen({super.key});
  33. @override
  34. Widget build(BuildContext context, WidgetRef ref) {
  35. ref.listen(authStoreProvider, (previous, next) {
  36. if (next.state == AuthState.authenticated) {
  37. context.goNamed(AppRoutePaths.home);
  38. } else if (next.state != AuthState.loading) {
  39. context.goNamed(AppRoutePaths.login);
  40. }
  41. });
  42. return const Scaffold(body: Center(child: CircularProgressIndicator()));
  43. }
  44. }