聆听风声 点点滴滴都很珍贵!

flutter应用退出时进行提示

2020-04-27

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

Comments

Content