flutter使用WillPopScope进行返回拦截

  • flutter使用WillPopScope进行返回拦截已关闭评论
  • 4 views
  • A+
所属分类:flutter

为了避免用户误触返回按钮而导致APP退出,在很多APP中都拦截了用户点击返回键的按钮,flutter是通过WillPopScope,其实flutter使用起来就非常简单如下:

 

return WillPopScope(
  onWillPop: _onWillPop,
  child:  Scaffold(

 

然后实现_onWillPop方法,如下:

Future<bool> _onWillPop() {
  return showDialog(
    context: context,
    builder: (context) => new AlertDialog(
      title: new Text('提示'),
      content: new Text('您好,确定退出app?'),
      actions: <Widget>[
        new FlatButton(
          onPressed: () => Navigator.of(context).pop(false),
          child: new Text(Constant.giveup),
        ),
        new FlatButton(
          onPressed: () => Navigator.of(context).pop(true),
          child: new Text(Constant.logout),
        ),
      ],
    ),
  ) ?? false;
}

 

效果图如下:

flutter使用WillPopScope进行返回拦截

 

 

就这么简单

 

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar