智慧水务管理系统 - 精河县供水工程综合管理平台

app_routes.dart 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'package:flutter/material.dart';
  2. import 'package:go_router/go_router.dart';
  3. import '../features/auth/pages/login_page.dart';
  4. import '../features/auth/services/token_service.dart';
  5. import '../features/main_shell/pages/main_shell_page.dart';
  6. import '../features/profile/pages/profile_page.dart';
  7. import '../features/revenue/pages/bill_list_page.dart';
  8. /// 应用路由配置
  9. class AppRoutes {
  10. static const String login = '/login';
  11. static const String main = '/main';
  12. static const String bills = '/bills';
  13. static const String profile = '/profile';
  14. static GoRouter createRouter() {
  15. return GoRouter(
  16. initialLocation: login,
  17. redirect: (BuildContext context, GoRouterState state) async {
  18. final tokenService = TokenService();
  19. final isLoggedIn = await tokenService.isLoggedIn();
  20. final isGoingToLogin = state.matchedLocation == login;
  21. if (!isLoggedIn && !isGoingToLogin) return login;
  22. if (isLoggedIn && isGoingToLogin) return main;
  23. return null;
  24. },
  25. routes: [
  26. GoRoute(
  27. path: login,
  28. builder: (context, state) => const LoginPage(),
  29. ),
  30. GoRoute(
  31. path: main,
  32. builder: (context, state) => const MainShellPage(),
  33. ),
  34. GoRoute(
  35. path: bills,
  36. builder: (context, state) => const BillListPage(),
  37. ),
  38. GoRoute(
  39. path: profile,
  40. builder: (context, state) => const ProfilePage(),
  41. ),
  42. ],
  43. );
  44. }
  45. }