AI_Drawing/lib/common/navigator_manager.dart
2024-06-03 15:30:15 +08:00

62 lines
1.5 KiB
Dart

import 'package:flutter/material.dart';
typedef OnInterceptor = Function(bool isInterceptor);
/// 导航管理
class NavigatorManager {
///所有路由栈,包括弹窗与页面
List<Route>? _history;
static final NavigatorManager _instance = NavigatorManager._internal();
factory NavigatorManager() => _instance;
NavigatorManager? _bus;
NavigatorManager._internal() {
if (_bus == null) {
_history = <Route>[];
}
}
///通过拦截器增加 push router
void addRouter(Route route) {
_history?.add(route);
}
///通过拦截器移动 push router
void removeRouter(Route route) {
_history?.remove(route);
}
///通过拦截器移动 push router
void replaceRouter(Route newRoute, Route oldRoute) {
_history?.remove(oldRoute);
_history?.add(newRoute);
}
List<Route>? get history => _history;
///是否顶层路由为页面,或者还未显示,正在压入栈
bool isTopRouter(String routerName) {
if (_history!.isEmpty) {
return false;
}
int i = _history!.length - 1;
Route route = _history![i];
while (route.settings == null && i >= 0) {
i--;
route = _history![i];
}
return route.settings.name != null && route.settings.name == routerName;
}
///是否顶层路由为弹窗(包括showBottomSheet),或者还未显示,正在压入栈
bool isTopDialog() {
if (_history!.isEmpty) {
return false;
}
return _history![_history!.length - 1].settings.name == null;
}
}