app_routes.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. import '../pages/breeding/bind_wing_tag_page.dart';
  12. import '../pages/breeding/cage_change_page.dart';
  13. import '../pages/breeding/individual_weighing_page.dart';
  14. import '../pages/breeding/individual_culling_page.dart';
  15. import '../pages/breeding/batch_culling_page.dart';
  16. class AppRouteNames {
  17. static const String splash = '/';
  18. static const String login = '/login';
  19. static const String home = '/home';
  20. static const String checkin = '/checkin';
  21. static const String checkinRecord = '/checkin_record';
  22. static const String bindwingTagNum = '/bind_wing_tag_num';
  23. static const String cageChange = '/cage_change';
  24. static const String individualWeighing = '/individual_weighing';
  25. static const String individualCulling = '/individual_culling';
  26. static const String batchCulling = '/batch_culling';
  27. static const String sample = '/sample';
  28. static const String sampleDetail = '/sample/detail';
  29. }
  30. class AppRoutes {
  31. static final List<GoRoute> routes = [
  32. GoRoute(
  33. path: AppRouteNames.splash,
  34. builder: (context, state) => const SplashScreen(),
  35. ),
  36. GoRoute(
  37. path: AppRouteNames.login,
  38. name: AppRouteNames.login,
  39. builder: (context, state) => const LoginPage(),
  40. ),
  41. GoRoute(
  42. path: AppRouteNames.home,
  43. name: AppRouteNames.home,
  44. builder: (context, state) => const HomePage(),
  45. ),
  46. GoRoute(
  47. path: AppRouteNames.checkin,
  48. name: AppRouteNames.checkin,
  49. builder: (context, state) => const CheckinPage(),
  50. ),
  51. GoRoute(
  52. path: '${AppRouteNames.checkinRecord}/:id',
  53. name: AppRouteNames.checkinRecord,
  54. builder: (context, state) {
  55. final id = state.pathParameters['id'] ?? '';
  56. return CheckinRecordPage(id: id);
  57. },
  58. ),
  59. GoRoute(
  60. path: AppRouteNames.sample,
  61. name: AppRouteNames.sample,
  62. builder: (context, state) => const SampleQueryPage(),
  63. ),
  64. GoRoute(
  65. path: '${AppRouteNames.sampleDetail}/:id',
  66. name: AppRouteNames.sampleDetail,
  67. builder: (context, state) {
  68. final id = state.pathParameters['id'] ?? '';
  69. return SampleDetailPage(id: id);
  70. },
  71. ),
  72. GoRoute(
  73. path: AppRouteNames.bindwingTagNum,
  74. name: AppRouteNames.bindwingTagNum,
  75. builder: (context, state) => const BindWingTagNumPage(),
  76. ),
  77. GoRoute(
  78. path: AppRouteNames.cageChange,
  79. name: AppRouteNames.cageChange,
  80. builder: (context, state) => const CageChangePage(),
  81. ),
  82. GoRoute(
  83. path: AppRouteNames.individualWeighing,
  84. name: AppRouteNames.individualWeighing,
  85. builder: (context, state) => const IndividualWeighingPage(),
  86. ),
  87. GoRoute(
  88. path: AppRouteNames.individualCulling,
  89. name: AppRouteNames.individualCulling,
  90. builder: (context, state) => const IndividualCullingPage(),
  91. ),
  92. GoRoute(
  93. path: AppRouteNames.batchCulling,
  94. name: AppRouteNames.batchCulling,
  95. builder: (context, state) => const BatchCullingPage(),
  96. ),
  97. ];
  98. }
  99. // 启动屏
  100. class SplashScreen extends ConsumerWidget {
  101. const SplashScreen({super.key});
  102. @override
  103. Widget build(BuildContext context, WidgetRef ref) {
  104. ref.listen(authStoreProvider, (previous, next) {
  105. if (next.state == AuthState.authenticated) {
  106. context.goNamed(AppRouteNames.home);
  107. } else if (next.state != AuthState.loading) {
  108. context.goNamed(AppRouteNames.login);
  109. }
  110. });
  111. return const Scaffold(body: Center(child: CircularProgressIndicator()));
  112. }
  113. }