70 lines
2.7 KiB
Dart
70 lines
2.7 KiB
Dart
import 'package:go_router/go_router.dart';
|
||
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
|
||
import 'package:maibu_satabot_v2/features/ai/presentation/routes/ai_routes.dart';
|
||
import 'package:maibu_satabot_v2/features/auth/presentation/pages/login_page.dart';
|
||
import 'package:maibu_satabot_v2/features/auth/presentation/routes/auth_routes.dart';
|
||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||
import 'package:maibu_satabot_v2/features/home/presentation/routes/home_routes.dart';
|
||
import 'package:maibu_satabot_v2/features/main_container/presentation/pages/tab_settings_page.dart';
|
||
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';
|
||
|
||
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';
|
||
import '../../features/main_container/presentation/main_wrapper.dart';
|
||
import 'go_router_refresh_stream.dart';
|
||
|
||
GoRouter createRouter(AuthCubit authCubit) {
|
||
return GoRouter(
|
||
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;
|
||
|
||
// 如果未登录,只允许访问 login 和 register 页面
|
||
if (!loggedIn && !loggingIn && !registering) {
|
||
return RoutePaths.login;
|
||
}
|
||
// 如果已登录,尝试访问 login 或 register,则跳转到首页
|
||
if (loggedIn && (loggingIn || registering)) {
|
||
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(
|
||
path: RoutePaths.home,
|
||
builder: (context, state) => const MainWrapper(),
|
||
routes: [
|
||
GoRoute(
|
||
path: 'settings',
|
||
builder: (context, state) => const TabSettingsPage(),
|
||
),
|
||
],
|
||
),
|
||
|
||
...AuthRoutes.routes,
|
||
...HomeRoutes.routes,
|
||
...AiRoutes.routes,
|
||
...MyRoutes.routes,
|
||
...AlarmCenterRoutes.routes,
|
||
],
|
||
);
|
||
}
|