routes.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:go_router/go_router.dart';
  3. import '../../providers/auth_provider.dart';
  4. import 'pages/login/init_page.dart';
  5. import 'pages/login/login_page.dart';
  6. import 'pages/dashboard/dashboard_page.dart';
  7. import 'pages/dashboard/agent_switch_page.dart';
  8. import 'pages/orders/order_list_page.dart';
  9. import 'pages/orders/order_edit_page.dart';
  10. import 'pages/customers/customer_list_page.dart';
  11. import 'pages/customers/customer_detail_page.dart';
  12. import 'pages/catalog/catalog_page.dart';
  13. import 'pages/promotions/promotion_list_page.dart';
  14. import 'pages/payments/payments_page.dart';
  15. import 'pages/statistics/statistics_page.dart';
  16. import 'pages/sync/sync_page.dart';
  17. import 'pages/sync/sync_settings_page.dart';
  18. final routerProvider = Provider<GoRouter>((ref) {
  19. final authState = ref.watch(authProvider);
  20. return GoRouter(
  21. initialLocation: '/login',
  22. redirect: (context, state) {
  23. final isLogin = state.matchedLocation == '/login';
  24. final isInit = state.matchedLocation == '/init';
  25. final isSyncSettings = state.matchedLocation == '/sync-settings';
  26. if (!authState.isInitialized && !isInit && !isSyncSettings) {
  27. return '/init';
  28. }
  29. if (authState.isInitialized &&
  30. !authState.isLoggedIn &&
  31. !isLogin &&
  32. !isSyncSettings) {
  33. return '/login';
  34. }
  35. if (authState.isLoggedIn && isLogin) return '/dashboard';
  36. if (authState.isLoggedIn && isInit) return '/dashboard';
  37. return null;
  38. },
  39. routes: [
  40. GoRoute(path: '/init', builder: (_, __) => const InitPage()),
  41. GoRoute(path: '/login', builder: (_, __) => const LoginPage()),
  42. GoRoute(
  43. path: '/sync-settings',
  44. builder: (_, __) => const SyncSettingsPage(),
  45. ),
  46. GoRoute(
  47. path: '/dashboard',
  48. builder: (_, __) => const DashboardPage(),
  49. routes: [
  50. GoRoute(path: 'orders', builder: (_, __) => const OrderListPage()),
  51. GoRoute(
  52. path: 'orders/new',
  53. builder: (_, state) {
  54. final idCliente = int.tryParse(
  55. state.uri.queryParameters['cliente'] ?? '',
  56. );
  57. return OrderEditPage(idCliente: idCliente);
  58. },
  59. ),
  60. GoRoute(
  61. path: 'orders/:id',
  62. builder: (_, state) =>
  63. OrderEditPage(id: int.parse(state.pathParameters['id']!)),
  64. ),
  65. GoRoute(
  66. path: 'customers',
  67. builder: (_, __) => const CustomerListPage(),
  68. routes: [
  69. GoRoute(
  70. path: ':id',
  71. builder: (_, state) => CustomerDetailPage(
  72. id: int.parse(state.pathParameters['id']!),
  73. ),
  74. ),
  75. ],
  76. ),
  77. GoRoute(path: 'catalog', builder: (_, __) => const CatalogPage()),
  78. GoRoute(
  79. path: 'promotions',
  80. builder: (_, __) => const PromotionListPage(),
  81. ),
  82. GoRoute(path: 'payments', builder: (_, __) => const PaymentsPage()),
  83. GoRoute(
  84. path: 'statistics',
  85. builder: (_, __) => const StatisticsPage(),
  86. ),
  87. GoRoute(path: 'sync', builder: (_, __) => const SyncPage()),
  88. GoRoute(
  89. path: 'sync/settings',
  90. builder: (_, __) => const SyncSettingsPage(),
  91. ),
  92. GoRoute(
  93. path: 'switch-agent',
  94. builder: (_, __) => const AgentSwitchPage(),
  95. ),
  96. ],
  97. ),
  98. ],
  99. );
  100. });