app_routes.dart 2.8 KB

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