app_routes.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. import '../pages/checkin/checkin_page.dart';
  8. import '../pages/sample_transfer/sample_transfer_page.dart';
  9. class AppRoutePaths {
  10. static const String splash = '/';
  11. static const String login = '/login';
  12. static const String home = '/home';
  13. static const String checkin = '/checkin';
  14. static const String sampleTransfer = '/sample_transfer';
  15. }
  16. class AppRoutes {
  17. static final List<GoRoute> routes = [
  18. GoRoute(
  19. path: AppRoutePaths.splash,
  20. builder: (context, state) => const SplashScreen(),
  21. ),
  22. GoRoute(
  23. path: AppRoutePaths.login,
  24. name: AppRoutePaths.login,
  25. builder: (context, state) => const LoginPage(),
  26. ),
  27. GoRoute(
  28. path: AppRoutePaths.home,
  29. name: AppRoutePaths.home,
  30. builder: (context, state) => const HomePage(),
  31. ),
  32. GoRoute(
  33. path: AppRoutePaths.checkin,
  34. name: AppRoutePaths.checkin,
  35. builder: (context, state) => const CheckinPage(),
  36. ),
  37. GoRoute(
  38. path: AppRoutePaths.sampleTransfer,
  39. name: AppRoutePaths.sampleTransfer,
  40. builder: (context, state) => const SampleTransferPage(),
  41. ),
  42. ];
  43. }
  44. // 启动屏
  45. class SplashScreen extends ConsumerWidget {
  46. const SplashScreen({super.key});
  47. @override
  48. Widget build(BuildContext context, WidgetRef ref) {
  49. ref.listen(authStoreProvider, (previous, next) {
  50. if (next.state == AuthState.authenticated) {
  51. context.goNamed(AppRoutePaths.home);
  52. } else if (next.state != AuthState.loading) {
  53. context.goNamed(AppRoutePaths.login);
  54. }
  55. });
  56. return const Scaffold(body: Center(child: CircularProgressIndicator()));
  57. }
  58. }