flutter 监听返回

在项目中遇到了一个场景,A页面必须返回某个tab页,但是A页面可能会调到B,再跳到C,最后回到A。这个时候A的返回肯定是C。

想了一些解决方案,都不如监听A页面的实体键返回或者虚拟键返回来的快速便捷。

 

代码:

 @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child:  Scaffold(
        backgroundColor: Color(0xfff7f7f7),
        appBar: AppBar(
          title: Text('优惠券'),
          centerTitle: true,
          elevation: 0.0,
        ),
      ),
      onWillPop: _requestPop
    );
  }

 

WillPopScope: 监听返回

  // 监听返回
  Future<bool> _requestPop() {
    pint('点击了返回按钮');
    // dosomething()
    return new Future.value(false);
  }

 

这样就可以在返回之前处理一些你想做的事情

 

posted @ 2019-07-09 19:25  _whys  阅读(4767)  评论(0编辑  收藏  举报