11
This commit is contained in:
1 parent
e485454d8d
commit
da3eee48c1
1 file changed
+38
-12
@@ -39,12 +39,18 @@ class _TerminalPageState extends State<TerminalPage> {
|
||||
Timer? _routeLoadingTimer;
|
||||
bool _routeLoading = false;
|
||||
bool _menuSyncScheduled = false;
|
||||
bool _routeStateSyncScheduled = false;
|
||||
String? _pendingRouteName;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_menuController = Get.find<TerminalMenuControl>();
|
||||
_initialRoute = _routeForSoleVisibleMenu() ?? RouteNames.cashier;
|
||||
final soleVisibleRoute = _routeForSoleVisibleMenu();
|
||||
// 设置页必须继续经过管理密码校验,不能因为它恰好是唯一菜单就在初始化路由中绕过守卫。
|
||||
_initialRoute = soleVisibleRoute == RouteNames.setting
|
||||
? RouteNames.cashier
|
||||
: soleVisibleRoute ?? RouteNames.cashier;
|
||||
_currentConfig = _terminalRoutes[_initialRoute]!;
|
||||
_menuWorker = everAll(
|
||||
[_menuController.menuOrder, _menuController.hiddenMenus],
|
||||
@@ -73,6 +79,11 @@ class _TerminalPageState extends State<TerminalPage> {
|
||||
child: Navigator(
|
||||
key: Get.nestedKey(TerminalNavigationTool.navigatorId),
|
||||
initialRoute: _initialRoute,
|
||||
// 内层路由名称包含 /terminal 前缀,默认生成逻辑会额外创建 / 和
|
||||
// /terminal 中间路由;这里只创建目标页,避免误装载收银页及其 Binding。
|
||||
onGenerateInitialRoutes: (_, initialRoute) => [
|
||||
_generateRoute(RouteSettings(name: initialRoute)),
|
||||
],
|
||||
observers: [_TerminalRouteObserver(_handleRouteChanged)],
|
||||
onGenerateRoute: _generateRoute,
|
||||
),
|
||||
@@ -111,21 +122,36 @@ class _TerminalPageState extends State<TerminalPage> {
|
||||
if (route == null || route == _currentConfig.name) return;
|
||||
|
||||
// 配置收敛到单入口后直接切换工作区,避免侧栏收起时仍停留在已隐藏页面。
|
||||
TerminalNavigationTool.replaceWorkspace(route);
|
||||
if (route == RouteNames.setting) {
|
||||
unawaited(TerminalNavigationTool.open(route));
|
||||
} else {
|
||||
TerminalNavigationTool.replaceWorkspace(route);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _handleRouteChanged(String? routeName) {
|
||||
final config = _terminalRoutes[routeName];
|
||||
if (config == null || config == _currentConfig || !mounted) return;
|
||||
_routeLoadingTimer?.cancel();
|
||||
setState(() {
|
||||
_currentConfig = config;
|
||||
_routeLoading = true;
|
||||
});
|
||||
// Loading 只遮住二级路由重建的短暂帧,页面自己的接口加载状态仍由 Controller 负责。
|
||||
_routeLoadingTimer = Timer(const Duration(milliseconds: 180), () {
|
||||
if (mounted) setState(() => _routeLoading = false);
|
||||
if (routeName == null || !_terminalRoutes.containsKey(routeName)) return;
|
||||
_pendingRouteName = routeName;
|
||||
if (_routeStateSyncScheduled) return;
|
||||
_routeStateSyncScheduled = true;
|
||||
// Navigator 会在自身构建和历史栈刷新期间通知 Observer,父页面状态必须延后更新。
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_routeStateSyncScheduled = false;
|
||||
if (!mounted) return;
|
||||
final pendingRouteName = _pendingRouteName;
|
||||
_pendingRouteName = null;
|
||||
final config = _terminalRoutes[pendingRouteName];
|
||||
if (config == null || config == _currentConfig) return;
|
||||
_routeLoadingTimer?.cancel();
|
||||
setState(() {
|
||||
_currentConfig = config;
|
||||
_routeLoading = true;
|
||||
});
|
||||
// Loading 只遮住二级路由重建的短暂帧,页面自己的接口加载状态仍由 Controller 负责。
|
||||
_routeLoadingTimer = Timer(const Duration(milliseconds: 180), () {
|
||||
if (mounted) setState(() => _routeLoading = false);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user