Home > Blockchain >  Flutter Navigator pop / remove all except the latest
Flutter Navigator pop / remove all except the latest

Time:11-14

In my flutter apps, there is a main page, when user is back to that page (regardless from where) they are not suppose to tap (or swipe) back to the previous page.

Currently we are using Navigator.pushAndRemoveUntil.

But there is a problem, on and off, we will have programmers forgot to remove all the routes before navigating back to the main page.

Is there a way for us to pop / clear or remove all the navigator's route at the main page itself?

CodePudding user response:

Use willPopScope and return false always like the following

 Widget build(BuildContext context) {
     return WillPopScope(
      onWillPop: () async => false,
      child:  Scaffold(
         appBar:  AppBar(
          title: Text("Home Page"),
         ),
         body:  Center(
          child:  Text("Home Page"),
         ),
       ),
     );
   }
  • Related