在 Flutter 中以编程方式调用 onBackpressed

Call onBackpress programatically in Flutter

如何在 Flutter 上以编程方式调用 backpress 函数 (onWillPop)。

在Android原生中我需要做的是在我需要模拟backpress点击时调用这个。

this.onBackPressed();

我的onBackPress函数,在WillPopScope:

里面设置为onWillPop
Future<bool> _onBackPressed() {
    return showDialog(
          context: context,
          builder: (context) => new AlertDialog(
                title: Text("Você tem certeza?"),
                content: Text('Você realmente quer sair do app'),
                actions: <Widget>[
                  new RaisedButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    child: Text("Não")
                  ),
                  new RaisedButton(
                    onPressed: _signOut,
                    child: Text("Sim")
                  ),
                ],
              ),
        ) ??
        false;
  }

它显示一个对话框,如果单击是,则应用程序注销。

我的想法是在 AppBar 上创建一个后退箭头来做同样的事情

如果你想调用 onWillPop 只需在 Navigator 上调用以下方法而不是 pop 方法。调用onWillPop会引起flutter.

Navigator.of(context).maybePop();