首页 > 解决方案 > Flutter WillPopScope 等待弹出窗口消失

问题描述

我用 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:

标签: flutter

解决方案


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

这是我的解决方案

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;
    }
  }

推荐阅读