Detect and stop user from leaving current page in Flutter?

I want to detect when a user tries to leave the current page and probably stop them from doing so in flutter.

>Solution :

welcome to stack overflow,
You can achieve this by using the WillPopScope Widget.
Below is a code sample to achieve this.

          child: Container(),
          onWillPop: () {
            if (allowPop) {
              return Future.value(true);
            } else {
              return Future.value(false);

Where allowPop can be a variable you can use to check if to allow user to leave the page or not.

Leave a Reply