Alexander Schurf
Alexander Schurf

Reputation: 85

How to block a page in Navigatior if it is already open?

I am just starting to learn flutter, faced a small problem and hope for help.

Navigator.popAndPushNamed(context, HomePage.routeName)

I have 3 pages: A, B and C. All of these pages are available in the Drawer dropdown menu. But if I am on page B, open my menu and select the same page again, it re-opens on top of the old page B.

I don't like this behavior and I would like to block the ability to open the page if it is active. How can I do it?

Upvotes: 1

Views: 170

Answers (1)

Akif
Akif

Reputation: 7640

Here is a simple solution for this. You can identify a currentPage variable and check it before the navigation like this:

  onTap: currentPage == HomePage.routeName 
         ? null 
         : () { 
           currentPage == HomePage.routeName; 
           Navigator.popAndPushNamed(context, HomePage.routeName);
         }
 

Upvotes: 1

Related Questions