app_routes.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_page.dart';
  6. import '../pages/home/home_page.dart';
  7. import '../pages/checkin/checkin_page.dart';
  8. import '../pages/checkin/checkin_record_page.dart';
  9. import '../pages/sample/sample_query_page.dart';
  10. import '../pages/sample/sample_detail_page.dart';
  11. class AppRouteNames {
  12. static const String splash = '/';
  13. static const String login = '/login';
  14. static const String home = '/home';
  15. static const String checkin = '/checkin';
  16. static const String checkinRecord = '/checkin_record';
  17. static const String sample = '/sample';
  18. static const String sampleDetail = '/sample/detail';
  19. }
  20. class AppRoutes {
  21. static final List<GoRoute> routes = [
  22. GoRoute(
  23. path: AppRouteNames.splash,
  24. builder: (context, state) => const SplashScreen(),
  25. ),
  26. GoRoute(
  27. path: AppRouteNames.login,
  28. name: AppRouteNames.login,
  29. builder: (context, state) => const LoginPage(),
  30. ),
  31. GoRoute(
  32. path: AppRouteNames.home,
  33. name: AppRouteNames.home,
  34. builder: (context, state) => const HomePage(),
  35. ),
  36. GoRoute(
  37. path: AppRouteNames.checkin,
  38. name: AppRouteNames.checkin,
  39. builder: (context, state) => const CheckinPage(),
  40. ),
  41. GoRoute(
  42. path: '${AppRouteNames.checkinRecord}/:id',
  43. name: AppRouteNames.checkinRecord,
  44. builder: (context, state) {
  45. final id = state.pathParameters['id'] ?? '';
  46. return CheckinRecordPage(id: id);
  47. },
  48. ),
  49. GoRoute(
  50. path: AppRouteNames.sample,
  51. name: AppRouteNames.sample,
  52. builder: (context, state) => const SampleQueryPage(),
  53. ),
  54. GoRoute(
  55. path: '${AppRouteNames.sampleDetail}/:id',
  56. name: AppRouteNames.sampleDetail,
  57. builder: (context, state) {
  58. final id = state.pathParameters['id'] ?? '';
  59. return SampleDetailPage(id: id);
  60. },
  61. ),
  62. ];
  63. }
  64. // 启动屏
  65. class SplashScreen extends ConsumerWidget {
  66. const SplashScreen({super.key});
  67. @override
  68. Widget build(BuildContext context, WidgetRef ref) {
  69. ref.listen(authStoreProvider, (previous, next) {
  70. if (next.state == AuthState.authenticated) {
  71. context.goNamed(AppRouteNames.home);
  72. } else if (next.state != AuthState.loading) {
  73. context.goNamed(AppRouteNames.login);
  74. }
  75. });
  76. return const Scaffold(body: Center(child: CircularProgressIndicator()));
  77. }
  78. }