58 lines
1.6 KiB
Dart
58 lines
1.6 KiB
Dart
|
|
import 'package:qhd_prevention/common/route_model.dart';
|
|||
|
|
/// 路由管理
|
|||
|
|
class RouteService {
|
|||
|
|
static final RouteService _instance = RouteService._internal();
|
|||
|
|
factory RouteService() => _instance;
|
|||
|
|
RouteService._internal();
|
|||
|
|
|
|||
|
|
// 存储所有路由配置
|
|||
|
|
List<RouteModel> _allRoutes = [];
|
|||
|
|
|
|||
|
|
// 获取主Tab路由(第一级children)
|
|||
|
|
List<RouteModel> get mainTabs => _allRoutes.isNotEmpty
|
|||
|
|
? _allRoutes.first.children
|
|||
|
|
: [];
|
|||
|
|
|
|||
|
|
// 初始化路由配置
|
|||
|
|
void initializeRoutes(List<dynamic> routeList) {
|
|||
|
|
_allRoutes = routeList.map((route) => RouteModel.fromJson(route)).toList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 根据路径查找路由
|
|||
|
|
RouteModel? findRouteByPath(String path) {
|
|||
|
|
for (final route in _allRoutes) {
|
|||
|
|
final found = _findRouteRecursive(route, path);
|
|||
|
|
if (found != null) return found;
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
RouteModel? _findRouteRecursive(RouteModel route, String path) {
|
|||
|
|
if (route.path == path) return route;
|
|||
|
|
for (final child in route.children) {
|
|||
|
|
final found = _findRouteRecursive(child, path);
|
|||
|
|
if (found != null) return found;
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 获取某个Tab下的所有可显示的路由(hasMenu为true的叶子节点)
|
|||
|
|
List<RouteModel> getRoutesForTab(RouteModel tab) {
|
|||
|
|
final routes = <RouteModel>[];
|
|||
|
|
_collectLeafRoutes(tab, routes);
|
|||
|
|
return routes;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void _collectLeafRoutes(RouteModel route, List<RouteModel> collector) {
|
|||
|
|
if (route.hasMenu) {
|
|||
|
|
collector.add(route);
|
|||
|
|
if (!route.isLeaf) {
|
|||
|
|
for (final child in route.children) {
|
|||
|
|
_collectLeafRoutes(child, collector);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|