pikapika/lib/basic/Navigator.dart

50 lines
1.2 KiB
Dart

/// 导航相关
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:pikapika/basic/config/IconLoading.dart';
// 用于监听返回到当前页面的事件
// (await Navigator.push 会在子页面pushReplacement时结束阻塞)
final RouteObserver<ModalRoute<void>> routeObserver =
RouteObserver<ModalRoute<void>>();
// 路径深度计数
const _depthMax = 15;
var _depth = 0;
var navigatorObserver = _NavigatorObserver();
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);
}
}
// 路径达到一定深度的时候使用 pushReplacement
Future<dynamic> navPushOrReplace(
BuildContext context, WidgetBuilder builder) async {
if (_depth < _depthMax) {
return Navigator.push(
context,
mixRoute(builder: builder),
);
} else {
return Navigator.pushReplacement(
context,
mixRoute(builder: builder),
);
}
}