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(),
              ],
            )),
      ),
    );
  }
}

猜你喜欢

转载自blog.csdn.net/qq_32319999/article/details/80333511
今日推荐