Home > Back-end >  How to pass data back from modal in flutter?
How to pass data back from modal in flutter?

Time:01-03

So basically I am using the showModalBottomSheet widget to show a full screen container that has a GestureDetector that runs this onTap:

onTap: () {
  final String testText = "Sup";
  Navigator.of(context).pop(testText);
}

This obviously returns the text when I await the result when I call showModalBottomSheet however, I would also like to set enableDrag: true so that we can swipe the modal away.

My question is:

How can I pass an argument/result back when doing a swipe to dismiss? With a function, I can simple do Navigator.of(context).pop(...) but when we swipe, there is no function and so therefore I can't figure out a way to pass arguments when we swipe to dismiss.

Thank you!

CodePudding user response:

When you swipe the pop() method gets called and there isn't anyway to override it but I figured out a way to handle this scenario:

You can use then() method on showModalBottomSheet() like this:

showModalBottomSheet(context: context, builder: (context) => SecondPage()).then((value) {
        str = "done";
        print("data: $str");
      });

Keep in mind that the value that future returns the value that gets returned in pop() method otherwise it is null.

CodePudding user response:

Use this widget and add Navigator.Pop.

->Use this WillPopScope

For More Information Visit Click Here

  • Related