首页 > 解决方案 > 导航器观察者在打开对话框时观察推送事件

问题描述

在这种情况下,我想将 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>))]

在其他情况下,当我触发其他对话框(不是整页)时,它不会触发推送事件是不是因为一个是整页而不是整页?有人能告诉我为什么吗?

标签: flutternavigation

解决方案


推荐阅读