flutter 监听系统返回按钮

使用WillPopScope即可,重点onWillPop方法,
Future<bool> _onWillPop()=>new Future.value(false); 由于要弹出dialog,我这里是禁掉返回按钮,当然也可以在这里做一下其他操作,
比如连续点击两次返回,又或者连续pop两次把dialog和页面一起返回掉。
class _DialogComponent extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    Future<bool> _onWillPop()=>new Future.value(false);
    return WillPopScope(
      onWillPop:_onWillPop,
      child: Dialog(
        child: new Container(
            height: 64.0,
            alignment: Alignment.center,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text(
                  "努力加载中",
                  style: TextStyle(fontSize: 15.0, color: Colors.black),
                ),
                new Container(
                  width: 10.0,
                  height: 1.0,
                ),
                new CircularProgressIndicator(),
              ],
            )),
      ),
    );
  }
}
    原文作者:ASH救世主
    原文地址: https://blog.csdn.net/qq_32319999/article/details/80333511
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞