在我的应用程序中,当我单击设备的后退按钮时,它正在关闭应用程序。我使用了Willpopscope,但无法正常工作。但是,当我创建一个示例项目时,它正在工作。谁能解释一下为什么它在现有应用程序中不起作用。我分享了我的代码。 (在Appbar中,后退箭头工作正常,但问题出在设备后退按钮上)

    Future<bool> _onWillPop() {
    return showDialog(
          context: context,
          builder: (context) => new AlertDialog(
                title: new Text('Are you sure?'),
                content: new Text('Unsaved data will be lost.'),
                actions: <Widget>[
                  new FlatButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    child: new Text('No'),
                  ),
                  new FlatButton(
                    onPressed: () => Navigator.of(context).pop(true),
                    child: new Text('Yes'),
                  ),
                ],
              ),
        ) ??
        false;
  }

@override
  Widget build(BuildContext context) {
    return new WillPopScope(
      onWillPop: _onWillPop,
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text(
            "On Back pressed",
            style: new TextStyle(color: Colors.white),
          ),
        ),
        body: new Center(
          child: new Text("Home Page"),
        ),
      ),
    );
  }

最佳答案

用此_onWillPop替换_onWillPop()async{ return false; }方法

关于flutter - 设备后退按钮无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56555890/

10-11 22:28
查看更多