pikapika/lib/basic/Navigator.dart

50 lines
1.2 KiB
Dart
Raw Normal View History

2021-11-15 08:26:53 +00:00
/// 导航相关
2021-09-29 23:57:09 +00:00
2021-11-15 08:26:53 +00:00
import 'dart:async';
2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
2023-01-31 10:50:51 +00:00
import 'package:pikapika/basic/config/IconLoading.dart';
2021-09-29 23:57:09 +00:00
2021-11-15 08:26:53 +00:00
// 用于监听返回到当前页面的事件
// (await Navigator.push 会在子页面pushReplacement时结束阻塞)
2021-09-29 23:57:09 +00:00
final RouteObserver<ModalRoute<void>> routeObserver =
RouteObserver<ModalRoute<void>>();
2021-11-15 08:26:53 +00:00
// 路径深度计数
2021-09-29 23:57:09 +00:00
const _depthMax = 15;
var _depth = 0;
2021-11-15 08:26:53 +00:00
var navigatorObserver = _NavigatorObserver();
2021-09-29 23:57:09 +00:00
class _NavigatorObserver extends NavigatorObserver {
@override
void didPop(Route route, Route? previousRoute) {
_depth--;
print("DEPTH : $_depth");
super.didPop(route, previousRoute);
}
@override
void didPush(Route route, Route? previousRoute) {
_depth++;
print("DEPTH : $_depth");
super.didPush(route, previousRoute);
}
}
2021-11-15 08:26:53 +00:00
// 路径达到一定深度的时候使用 pushReplacement
Future<dynamic> navPushOrReplace(
BuildContext context, WidgetBuilder builder) async {
if (_depth < _depthMax) {
return Navigator.push(
context,
2023-01-31 10:50:51 +00:00
mixRoute(builder: builder),
2021-11-15 08:26:53 +00:00
);
} else {
return Navigator.pushReplacement(
context,
2023-01-31 10:50:51 +00:00
mixRoute(builder: builder),
2021-11-15 08:26:53 +00:00
);
}
}