单击后退按钮时如何执行 [英] Flutter how to execute when clicking back button

查看:97
本文介绍了单击后退按钮时如何执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从底部选项卡导航调用的页面,该页面执行initState函数,然后我通过单击按钮导航到具有详细信息和要执行的操作的页面,但是当我单击后退按钮转到原始页面时, initState不会再次运行,我发现由于Flutter不会在您将页面放在顶部时破坏页面,因为NAV不在菜单中而不是在新页面上,因此如何使initState在单击返回时再次运行按钮,还是有另一种方法可以侦听从后退按钮导航并运行需要更新数据的代码?

I have a page that is called from bottom tab nav which executes a initState function, I then navigate to a page via button click that has details and actions to take, however when I click the back button to goto originating page, the initState does not run again, I have found out that because Flutter does not destroy the page when you put one on top, since the NAV is not on new page as its not in the menu, how do I make initState run again on clicking back button or is there another way to listen for that navigate from back button and run the code that needs to update data?

任何帮助都会很棒。

推荐答案

您可以覆盖 AppBar 上的默认后退箭头,然后指定所需的值希望在调用 Navigator.pop 时返回以触发状态更改:

You can override the default back arrow on the AppBarand then specify the value you would like to return to trigger the change of the state when Navigator.pop is called:

伪-代码

因此,您需要在 onPressed 导航按钮的回调

so you need to have something like this in your onPressed callback of your navigation button

onPressed: ()async{
            var nav = await Navigator.of(context).push(newRoute);
            if(nav==true||nav==null){
              //change the state
            }
          },

,在您的newRoute中,您应该有这样的内容

and in your newRoute you should have something like this

new AppBar(
        leading: new IconButton(
          icon: new Icon(Icons.arrow_back),
          onPressed: (){Navigator.pop(context,true)}
        ),

我正在检查两个值 null true ,因为当用户在android屏幕上(屏幕底部的一个)点击BackButton时返回null值。我也相信null也会返回在Flutter中使用默认的BackButton,因此您实际上不需要覆盖 leading 属性,但是我没有自己检查一下,因此可能值得检查。

I am checking for both values null or true because the null value is returned when the user hits the BackButton on android screen (the one in the bottom of the screen). I also believe the null will also be returned with the default BackButton in Flutter so you do not actually need to override the leading property, but I have not checked that myself, so it may be worth checking.

这篇关于单击后退按钮时如何执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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