How to detect it is a base route using Navigator API?

18 views Asked by At

How to detect it is a base route using Navigator API? The idea is to show a snackbar with a message when that happens.

For example, when user returned to the base route then if back button will be pressed one more time then the app is closed and notifying the user about it is useful.

1

There are 1 answers

0
Kaushik Chandru On

You can use WillPopScope. Where you can control back button click

class HomePage extends StatelessWidget { 

  const HomePage({ Key? key }) : super(key: key); 

  

  @override 

  Widget build(BuildContext context) { 

  

    // WillPopScope Widget class used  

    return WillPopScope( 

      child: Scaffold( 

        backgroundColor: Colors.blue, 

          body: const Center(), 

      ),  

  

      // function callback that  

      // controls back button usage 

      onWillPop: () async { 

        return false; 
       //Here you can write pop up or snackbar 

      } 

      ); 

  } 
}