user14581222
user14581222

Reputation:

Flutter WillPopScope await for Popup Disappear

I Wrapped my Widget tree with WillPopScope and I want to show Popup Dialog which shows users to Exit from the app or not but at the same time, I have to use if()else{} condition. When I use this condition my logic works fine but didn't return anything.

What is trying to Achieve:

when someone clicks on the back press it checks the condition if it's true then Popup Appear and after the popup disappears it return some boolean value to WillPopScope but when the popup disappears WillPopScope didn't do anything.

I tried different solutions but didn't work for me.

Here's my code back press

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 Code is:

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

Upvotes: 0

Views: 1226

Answers (2)

Tasnuva Tavasum oshin
Tasnuva Tavasum oshin

Reputation: 4750

WillPopScope(
      onWillPop: () async {return await backPressed();

},
      child:

Change your Code to this . it will work now . thanks

Upvotes: 0

user14581222
user14581222

Reputation:

I Solved this problem. Just Use Future

Here's my solution

WillPopScope Code:

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

my back press code:

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

Upvotes: 1

Related Questions