flutter - 导航器观察者在打开对话框时观察推送事件
问题描述
在这种情况下,我想将 willpopscope 添加到我的应用程序中,我发现有时它无法正确弹出,所以我调试了代码,发现打开对话框时观察者观察到推送事件,我觉得这很奇怪。
这是我的观察者
class MyNavigatorObserver extends NavigatorObserver {
List<Route<dynamic>> routeStack = List();
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
previous = previousRoute;
current = route;
routeStack?.add(route);
print('didPush');
print(routeStack.length);
print(routeStack.sublist(0));
}
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
if (routeStack.length > 0) {
routeStack?.removeLast();
print('didPop');
print(routeStack.length);
print(routeStack.sublist(0));
}
}
@override
void didRemove(Route route, Route previousRoute) {
routeStack?.removeLast();
}
@override
void didReplace({Route newRoute, Route oldRoute}) {
routeStack?.removeLast();
routeStack?.add(newRoute);
}
}
我在应用程序页面的主类中使用它
WillPopScope(
...
child: Navigator(
observers: [MyNavigatorObserver],
key: _navigator,
onGenerateRoute: (RouteSettings settings) {
...
}
...
if (page.parentRoute != null) {
return MaterialPageRoute(
...
);
} else {
NaviArguments arg = settings.arguments;
return PageRouteBuilder(
...
);
}
},
),
),
当我触发一个显示对话框的动作时
void showProfileChildView(
BuildContext context, {
ProfileChildBuilder child,
ProfileViewState state,
}) {
showGeneralDialog(
context: context,
barrierDismissible: false,
transitionDuration: Duration(milliseconds: 150),
pageBuilder: (BuildContext context, Animation<double> animation, _) {
var scaleAnimation = Tween(
begin: Offset(0.0, 1.0),
end: Offset(0.0, 0.0),
).chain(CurveTween(curve: Curves.easeOutExpo)).animate(animation);
return SlideTransition(
position: scaleAnimation,
child: ProfileChildView(
child: child,
profileViewState: state,
),
);
},
useRootNavigator: false,
);
}
它打印出这个
I/flutter (25147): didPush
I/flutter (25147): 1
I/flutter (25147): [_DialogRoute<dynamic>(RouteSettings("null", null), animation: AnimationController#48fc4(▶ 0.000; for _DialogRoute<dynamic>))]
在其他情况下,当我触发其他对话框(不是整页)时,它不会触发推送事件是不是因为一个是整页而不是整页?有人能告诉我为什么吗?
解决方案
推荐阅读
- java - Microsoft Edge 的“无效参数:'handle' 必须是字符串”错误以及如何添加“w3c:false”功能?
- python - 如何使用 Python 中的 Open Street Map 获取附近的道路信息?
- java - 使用eclipse和MySql hibernate在java中自动生成带有卷边距离的序列键
- javascript - 根据其他数组过滤数组
- swiftui - 在 SwiftUI 中向左而不是向右导航?
- firebase - 使用flutter firebase的应用程序内部通知
- javascript - 当我使用 Observables 时 Angular ViewChild 不工作
- repository - 干净架构层方法中的网络请求轮询逻辑布局
- python - 在 Pandas 中,如何测试一个值是否存在于给定唯一 ID 的两个数据框中?
- python - 奇怪的 gzip - 几乎提取,但不完全正确