【问题标题】:Flutter WillPopScope await for Popup DisappearFlutter WillPopScope 等待弹出窗口消失
【发布时间】:2021-05-01 02:11:20
【问题描述】:

我用 WillPopScope 包装了我的小部件树,我想显示弹出对话框,显示用户是否退出应用程序,但同时,我必须使用 if()else{} 条件。当我使用此条件时,我的逻辑工作正常,但没有返回任何内容。

正在努力实现的目标:

当有人点击后按时,它会检查条件是否为真,然后弹出弹出窗口,在弹出窗口消失后,它会向 WillPopScope 返回一些布尔值,但当弹出窗口消失时,WillPopScope 没有做任何事情。

我尝试了不同的解决方案,但对我不起作用。

这是我的代码回压

backPressed() {
    if (isCollapsed) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12.0),
          ),
          clipBehavior: Clip.antiAliasWithSaveLayer,
          backgroundColor: Colors.white,
          actionsPadding: EdgeInsets.symmetric(horizontal: 12.0),
          title: Text(
            'Are you sure you want to close this App?',
            style: TextStyle(
              color: Colors.black.withOpacity(0.7),
            ),
          ),
          actions: <Widget>[
            RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5.0),
                ),
                child: Text('Exit'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                  print("Return True");
                  return true;
                }),
            RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5.0),
                ),
                child: Text('Dismiss'),
                onPressed: () {
                  Navigator.of(context).pop(false);
                  print("Return False");
                  return false;
                }),
          ],
        ),
      );
    } else {
      menuButton();
      return false;
    }
  }

WillPopScope 代码为:

WillPopScope(
      onWillPop: () async => backPressed(),
      child:

【问题讨论】:

  • showDialog 返回一个Future - 将其用作onWillPop 回调的返回值
  • 能否提供代码示例?
  • return showDialog(...) - 顺便说一句,您可以将代码缩短为 onWillPop: backPressed,
  • 是的,我的问题解决了。感谢您的帮助

标签: flutter


【解决方案1】:

我解决了这个问题。只使用未来

这是我的解决方案

WillPopScope 代码:

WillPopScope(
      onWillPop: () async {
        return await backPressed();
      },
      child:

我的背压代码:

  Future<bool> backPressed() async {
    if (isCollapsed) {
      bool value = await showDialog(
        context: context,
        builder: (context) => AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12.0),
          ),
          clipBehavior: Clip.antiAliasWithSaveLayer,
          backgroundColor: Colors.white,
          actionsPadding: EdgeInsets.symmetric(horizontal: 12.0),
          title: Text(
            'Are you sure you want to close this App?',
            style: TextStyle(
              color: Colors.black.withOpacity(0.7),
            ),
          ),
          actions: <Widget>[
            RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5.0),
                ),
                child: Text('Exit'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                  print("Return True");
                  return true;
                }),
            RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5.0),
                ),
                child: Text('Dismiss'),
                onPressed: () {
                  Navigator.of(context).pop(false);
                  print("Return False");
                  return false;
                }),
          ],
        ),
      );
      return value;
    } else {
      menuButton();
      return false;
    }
  }

【讨论】:

  • 您不需要bool value = await showDialog(,只需拨打return showDialog(
  • Future&lt;bool&gt; foo(BuildContext ctx) async { return showDialog(context: ctx, builder: (ctx) =&gt; AlertDialog(actions: [ TextButton(onPressed: () =&gt; Navigator.of(ctx).pop(true), child: Text('true')), TextButton(onPressed: () =&gt; Navigator.of(ctx).pop(false), child: Text('false')), ],)); }
  • 好的。我明白了,非常感谢您的帮助
【解决方案2】:
WillPopScope(
      onWillPop: () async {return await backPressed();

},
      child:

将您的代码更改为此 .它现在可以工作了。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 2020-02-20
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多