Detect and stop user from leaving current page in Flutter?

249

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

WillPopScope(
          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.

Share:
249
Malik Muhangi
Author by

Malik Muhangi

Updated on January 05, 2023

Comments

  • Malik Muhangi
    Malik Muhangi over 1 year

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