Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a page is open/added to Navigator stack?

I have three pages each one is a StatefulWidget put into separate dart files named:

welcome.dart
page_01.dart
page_02.dart

I open each page using pushNamed function of Navigator like so:

Navigator.of(context).pushNamed('/pageName');

this is made possible using custom RouteGenerator added to my app main MaterialApp widget.

Lets say I open welcome page, then page_01 then page_02 in this order. now I am looking at page_02 which is at the top of Navigation stack. I want to know if page_01 is open/added to Navigation stack and its index number, which in this case should have index of 1.

How to know if a page is open in navigation stack and get its index number ?

like image 988
Rami Ibrahim Avatar asked Dec 18 '19 23:12

Rami Ibrahim


Video Answer


1 Answers

The state class of Navigator holds a list of widgets named List<Route<dynamic>> _history.

Since this value is kept private and there is no public getter provided, we can not find what is the status of pushed widgets at any given moment.

However, NavigatorState does provide some getters to know the status of the extreme ends of _history:

  1. bool get isCurrent // if the current widget/route is at the top of the stack
  2. bool get isActive // if the current widget/route is on the navigator
  3. bool get isFirst // if the current widget/route is at the bottom of the stack

Since none of them takes any widget/route as parameters these getters only use this to produce results for each status queries. There is no way we can get information about widgets that are in the stack but are not being rendered at the moment of the query.

I am afraid I couldn't find any other details to get more about status of other routes.

I have referred to navigator.dart file for these details since most of the properties are kept private, online API documentation is not helpful.

If somebody can come up with more details, just let me know. :D

like image 105
Harshvardhan Joshi Avatar answered Sep 27 '22 22:09

Harshvardhan Joshi