Flutter-NavigatorObserver类如何工作? [英] Flutter - How the NavigatorObserver class works?

查看:358
本文介绍了Flutter-NavigatorObserver类如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检测导航器何时执行基本操作( push pop ),我发现该类可以实现这是 NavigatorObserver 类的,但是我找不到示例关于它如何工作的.

I'm trying to detect when the navigator performs the basic operations (push and pop), I found that the class to achieve this is with the NavigatorObserver class, but I can't find an example on how it works.

我已经尝试实现该接口:

I already tried implements the interface:

class MyClassState extends State<MyClass> with NavigatorObserver{

    ....

    @override
    void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
        print('This is never get called');
    }

    @override
    void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
        print('This is never get called');
    }
}

我当然打电话给Navigator.of(context).push(...); 但是重写方法永远不会被调用.

Of course I'm calling Navigator.of(context).push(...); But the override methods, never get called.

我怀疑与导航器的绑定丢失了,但是我不确定.

I suspect the binding with the navigator is missing, but I'm not sure.

有什么建议吗?

提前谢谢!

我认为我想做的事情很简单,但是我不知道该怎么做.

I think what I want to do is very simple but I can't figure out how to do it.

我有一个页面A ,可以按页面B .

页面B 上,我有一个定期计时器,必须在其中 (在页面B 上).

On page B I have a periodic timer that must be on there (on page B).

我要做的就是在弹出 B页(使用Android上的后退按钮或应用栏中的后退按钮)时,用timer.cancel()取消计时器. >页面B 弹出,即使页面B 消失了,计时器仍在执行.

All what I want to do is to cancel the timer with timer.cancel() when the page B get popped out (with back button on Android or back button in appbar), since when the page B get popped out the timer still executing even if the page B has gone.

  • 由于我不处理后退按钮(应用程序栏中的后退按钮和Android后退按钮),因此我无法通过Navigator.pop()将计时器作为参数传递.

-我找到了解决问题的方法,我所做的就是取消

-I found a way that solves the problem, what I did was cancel the timer on the

@override
void dispose() {
  timer.cancel();
  super.dispose();
}

方法.但是我不确定这是否是实现它的最佳方法.

method. But I'm not sure if this is the best way to accomplish it.

注意:

通过这种解决方法,如果我按Home(主页)按钮或在Android上打开多任务,计时器仍将执行,当返回应用程序时,计时器将重新启动,但计时器仍将执行其代码.

With this workaround the timer still executing if I press the Home button or open the multitask on Android, and when go back to the app this are restarted but again the timer still executing its code.

推荐答案

大多数时候,您不需要实现NavigatorObserver.请参阅其他StackOverflow答案,其中说明了如何使用pushpop在路由之间传递信息.在您描述的用例中,应该使用

Most of the time you don't need to implement NavigatorObserver. See this other StackOverflow answer explaining how to use push and pop to pass information between routes. In the use case you've described, you should addListener to an AnimationController, using the TickerProviderStateMixin to obtain a suitable vsync object. This ensures that your callback will not fire when the app is suspended or after your State is disposed. (Instead of addListener, you could use an AnimatedBuilder or AnimatedWidget the primary purpose of your callback is to rebuild a section of the widget tree.)

想要NavigatorObserver的主要时间是使用的是Firebase Analytics这样的插件.您可以在中查看用法示例.插件回购.您将navigatorObservers参数中的NavigatorObserver传递给MaterialApp构造函数:

The main time when you'd want a NavigatorObserver is if you're using a plugin like Firebase Analytics. You can see an example usage in the plugins repo. You pass the NavigatorObserver in the navigatorObservers argument to the MaterialApp constructor:

static FirebaseAnalyticsObserver observer =
  new FirebaseAnalyticsObserver(analytics: analytics);
...
return new MaterialApp(
  navigatorObservers: <NavigatorObserver>[observer],
  ...
);

具有实现NavigatorObserverState是不寻常的,因为您的MaterialApp应该位于窗口小部件层次结构的顶部附近.在构造它时,大多数State对象都不存在,因此很难将它们放入navigatorObservers数组中.您可以改用不是State的类.如有必要,可以使用GlobalKey<MyClassState>查找需要通知的State(但是,如果这样做,可能会有更简单的方法来完成所需的操作).

It is unusual to have a State that implements NavigatorObserver because your MaterialApp should be near the top of the widget hierarchy. At the time you're constructing it, most State objects won't exist yet so you'll have a hard time putting them into the navigatorObservers array. You might instead use a class that isn't a State. If necessary, you can use GlobalKey<MyClassState> to find the State that needs to be notified (but if you're doing this, there might be an easier way to accomplish what you want).

这篇关于Flutter-NavigatorObserver类如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆