Files
flutterApp/lib/core/router/app_router.dart

84 lines
3.4 KiB
Dart
Raw Normal View History

2026-06-01 17:33:16 +08:00
import 'package:flutter/foundation.dart';
2026-01-11 19:42:22 +08:00
import 'package:go_router/go_router.dart';
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
2026-01-18 20:21:14 +08:00
import 'package:maibu_satabot_v2/features/ai/presentation/routes/ai_routes.dart';
import 'package:maibu_satabot_v2/features/auth/presentation/pages/login_page.dart';
2026-01-18 20:21:14 +08:00
import 'package:maibu_satabot_v2/features/auth/presentation/routes/auth_routes.dart';
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
2026-01-18 20:21:14 +08:00
import 'package:maibu_satabot_v2/features/home/presentation/routes/home_routes.dart';
2026-05-18 17:18:58 +08:00
import 'package:maibu_satabot_v2/features/main_container/presentation/pages/tab_settings_page.dart';
2026-01-18 20:21:14 +08:00
import 'package:maibu_satabot_v2/features/my/presentation/routes/my_routes.dart';
import 'package:maibu_satabot_v2/features/v2/waring_center/presentation/routes/alarm_center_routes.dart';
2026-08-07 08:49:29 +08:00
import 'package:maibu_satabot_v2/features/v2/workorder/presentation/routes/workorder_routes.dart';
2026-01-11 19:42:22 +08:00
import '../../features/auth/presentation/bloc/auth_cubit.dart';
import '../../features/auth/presentation/bloc/auth_state.dart';
import '../../features/machine_details/presentation/pages/machine_details_page.dart';
2026-01-18 20:21:14 +08:00
import '../../features/main_container/presentation/main_wrapper.dart';
import '../../main.dart'; // 🔥 导入 navigatorKey
2026-01-11 19:42:22 +08:00
import 'go_router_refresh_stream.dart';
GoRouter createRouter(AuthCubit authCubit) {
2026-06-01 17:33:16 +08:00
// 调试:打印所有路由
debugPrint('🔍 [Router] 开始创建路由配置...');
2026-08-07 08:49:29 +08:00
debugPrint(
'🔍 [Router] AlarmCenterRoutes.routes 数量: ${AlarmCenterRoutes.routes.length}',
);
2026-06-01 17:33:16 +08:00
for (var route in AlarmCenterRoutes.routes) {
debugPrint('🔍 [Router] 路由: ${route.toString()}');
}
2026-08-07 08:49:29 +08:00
2026-01-11 19:42:22 +08:00
return GoRouter(
navigatorKey: navigatorKey, // 🔥 绑定全局 navigatorKey,使异地登录弹窗能获取 context
2026-01-11 19:42:22 +08:00
initialLocation: RoutePaths.login,
refreshListenable: GoRouterRefreshStream(authCubit.stream),
redirect: (context, state) {
final loggedIn = authCubit.state is AuthAuthenticated;
final loggingIn = state.matchedLocation == RoutePaths.login;
final registering = state.matchedLocation == RoutePaths.register;
2026-01-11 19:42:22 +08:00
// 如果未登录,只允许访问 login 和 register 页面
if (!loggedIn && !loggingIn && !registering) {
2026-01-11 19:42:22 +08:00
return RoutePaths.login;
}
// 如果已登录,尝试访问 login 或 register,则跳转到首页
if (loggedIn && (loggingIn || registering)) {
2026-01-11 19:42:22 +08:00
return RoutePaths.home;
}
return null;
},
routes: [
// 注册 machineDetails 路由
GoRoute(
path: RoutePaths.machineDetails,
name: 'machineDetails', // 建议加 name,方便跳转
builder: (context, state) {
// 从 extra 中取出设备数据(核心)
final DeviceEntity? device = state.extra as DeviceEntity?;
// 跳转到详情页组件,并传入设备数据
return MachineDetailsPage(device: device);
},
),
GoRoute(
2026-05-13 10:34:07 +08:00
path: RoutePaths.home,
builder: (context, state) => const MainWrapper(),
2026-05-18 17:18:58 +08:00
routes: [
GoRoute(
path: 'settings',
builder: (context, state) => const TabSettingsPage(),
),
],
),
2026-01-18 20:21:14 +08:00
...AuthRoutes.routes,
...HomeRoutes.routes,
...AiRoutes.routes,
...MyRoutes.routes,
...AlarmCenterRoutes.routes,
2026-08-07 08:49:29 +08:00
...WorkOrderRoutes.routes,
2026-01-11 19:42:22 +08:00
],
);
}