| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import 'package:chicken_farm/stores/auth_store.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:go_router/go_router.dart';
- import '../pages/account/login_page.dart';
- import '../pages/home/home_page.dart';
- import '../pages/checkin/checkin_page.dart';
- import '../pages/checkin/checkin_record_page.dart';
- import '../pages/sample/sample_query_page.dart';
- import '../pages/sample/sample_detail_page.dart';
- class AppRouteNames {
- static const String splash = '/';
- static const String login = '/login';
- static const String home = '/home';
- static const String checkin = '/checkin';
- static const String checkinRecord = '/checkin_record';
- static const String sample = '/sample';
- static const String sampleDetail = '/sample/detail';
- }
- class AppRoutes {
- static final List<GoRoute> routes = [
- GoRoute(
- path: AppRouteNames.splash,
- builder: (context, state) => const SplashScreen(),
- ),
- GoRoute(
- path: AppRouteNames.login,
- name: AppRouteNames.login,
- builder: (context, state) => const LoginPage(),
- ),
- GoRoute(
- path: AppRouteNames.home,
- name: AppRouteNames.home,
- builder: (context, state) => const HomePage(),
- ),
- GoRoute(
- path: AppRouteNames.checkin,
- name: AppRouteNames.checkin,
- builder: (context, state) => const CheckinPage(),
- ),
- GoRoute(
- path: '${AppRouteNames.checkinRecord}/:id',
- name: AppRouteNames.checkinRecord,
- builder: (context, state) {
- final id = state.pathParameters['id'] ?? '';
- return CheckinRecordPage(id: id);
- },
- ),
- GoRoute(
- path: AppRouteNames.sample,
- name: AppRouteNames.sample,
- builder: (context, state) => const SampleQueryPage(),
- ),
- GoRoute(
- path: '${AppRouteNames.sampleDetail}/:id',
- name: AppRouteNames.sampleDetail,
- builder: (context, state) {
- final id = state.pathParameters['id'] ?? '';
- return SampleDetailPage(id: id);
- },
- ),
- ];
- }
- // 启动屏
- class SplashScreen extends ConsumerWidget {
- const SplashScreen({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- ref.listen(authStoreProvider, (previous, next) {
- if (next.state == AuthState.authenticated) {
- context.goNamed(AppRouteNames.home);
- } else if (next.state != AuthState.loading) {
- context.goNamed(AppRouteNames.login);
- }
- });
- return const Scaffold(body: Center(child: CircularProgressIndicator()));
- }
- }
|