flutter应用退出时进行提示


通过WillPopScope处理应用

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onBackPressed,
      child: _child(),
    );
  }

提示方法

  Future<bool> _onBackPressed() {
    return showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Text('确定退出程序吗?'),
          actions: <Widget>[
            FlatButton(
              child: Text('暂不'),
              onPressed: () => Navigator.pop(context, false),
            ),
            FlatButton(
              child: Text('确定'),
              onPressed: () => Navigator.pop(context, true),
            ),
          ],
        ),
    );
  }

发表评论

发表评论