Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

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:
question from:https://stackoverflow.com/questions/65915818/flutter-willpopscope-await-for-popup-disappear

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...