是否可以在某个特定时间点告诉我们 Navigator
是否处于过渡过程中?
问题的上下文是我想确保当前没有在屏幕上(向后或向前)进行过渡动画。
答案 0 :(得分:1)
您无法从导航器实例知道这一点。导航器不知道转换。这是Route的工作,而不是Navigator。
要解决您的需求,您需要做两件事:
这是一个完整的NavigatorObserver,它可以获取所有路线,并公开获取器以了解当前是否有动画:
class AllRoutes extends ValueNotifier<List<Route>> with NavigatorObserver {
AllRoutes() : super(const []);
bool _isAnimating(Animation a) =>
a.status == AnimationStatus.forward ||
a.status == AnimationStatus.reverse;
NavigationTransitionStatus get animationStatus {
for (final route in value) {
if (route is ModalRoute &&
(_isAnimating(route.animation) ||
_isAnimating(route.secondaryAnimation))) {
return NavigationTransitionStatus.animating;
}
}
return NavigationTransitionStatus.idle;
}
@override
void didPush(Route route, Route previousRoute) {
value = [...value, route];
}
@override
void didReplace({Route newRoute, Route oldRoute}) {
value = [...value]..[value.indexOf(oldRoute)] = newRoute;
}
@override
void didPop(Route route, Route previousRoute) {
value = [...value]..remove(route);
}
@override
void didRemove(Route route, Route previousRoute) {
value = [...value]..remove(route);
}
}
您必须先将其传递给等效的“ MaterialApp”:
final myObserver = AllRoutes();
MaterialApp(
navigatorObservers: [myObserver],
)
您可以执行以下操作:
myObserver.animationStatus == NavigationTransitionStatus.animating;