【问题标题】:Call onBackpress programatically in Flutter在 Flutter 中以编程方式调用 onBackpressed
【发布时间】:2020-07-23 11:53:39
【问题描述】:

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

在 Android Native 中,我需要做的是在需要模拟后按点击时调用它。

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 上创建一个后退箭头来做同样的事情

【问题讨论】:

  • 回压功能你想要什么?
  • 或者你想在appbar中创建返回按钮?

标签: android ios flutter back onbackpressed


【解决方案1】:

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

Navigator.of(context).maybePop();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多