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';
|
2026-02-25 13:41:23 +08:00
|
|
|
|
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';
|
2026-02-26 16:38:10 +08:00
|
|
|
|
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';
|
|
|
|
|
|
import 'package:maibu_satabot_v2/features/my/presentation/routes/my_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';
|
2026-02-25 13:41:23 +08:00
|
|
|
|
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';
|
2026-01-11 19:42:22 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
if (!loggedIn && !loggingIn) {
|
|
|
|
|
|
return RoutePaths.login;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (loggedIn && loggingIn) {
|
|
|
|
|
|
return RoutePaths.home;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
},
|
|
|
|
|
|
routes: [
|
2026-02-25 13:41:23 +08:00
|
|
|
|
// 注册 machineDetails 路由
|
|
|
|
|
|
GoRoute(
|
|
|
|
|
|
path: RoutePaths.machineDetails,
|
2026-02-26 16:38:10 +08:00
|
|
|
|
name: 'machineDetails', // 建议加 name,方便跳转
|
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
|
// 从 extra 中取出设备数据(核心)
|
|
|
|
|
|
final DeviceEntity? device = state.extra as DeviceEntity?;
|
|
|
|
|
|
|
|
|
|
|
|
// 跳转到详情页组件,并传入设备数据
|
|
|
|
|
|
return MachineDetailsPage(device: device);
|
|
|
|
|
|
},
|
2026-02-25 13:41:23 +08:00
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
|
|
// 注册登录注册 路由
|
|
|
|
|
|
GoRoute(
|
|
|
|
|
|
path: RoutePaths.machineDetails,
|
|
|
|
|
|
builder: (context, state) => const LoginPage(), // 替换为实际页面
|
|
|
|
|
|
),
|
|
|
|
|
|
|
2026-01-18 20:21:14 +08:00
|
|
|
|
// 1. 外部路由(无底部导航栏)
|
|
|
|
|
|
...AuthRoutes.routes,
|
|
|
|
|
|
...HomeRoutes.routes,
|
|
|
|
|
|
...AiRoutes.routes,
|
|
|
|
|
|
...MyRoutes.routes,
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 内部路由(带底部导航栏的容器)
|
|
|
|
|
|
StatefulShellRoute.indexedStack(
|
|
|
|
|
|
builder: (context, state, navigationShell) {
|
|
|
|
|
|
// 返回我们定义的 MainWrapper
|
|
|
|
|
|
return MainWrapper(navigationShell: navigationShell);
|
|
|
|
|
|
},
|
|
|
|
|
|
branches: [
|
|
|
|
|
|
// 拿一级页面
|
|
|
|
|
|
HomeRoutes.branch,
|
|
|
|
|
|
|
|
|
|
|
|
AiRoutes.branch,
|
|
|
|
|
|
|
|
|
|
|
|
MyRoutes.branch,
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
2026-01-11 19:42:22 +08:00
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|