Reputation: 473
I have a card with PopupMenuButton that I can tap to perform the 'copy' action. I can also perform the same action from the card details page. The problem I have is to get back to the home page I have to cater for both paths. So if the action is performed from the details page, I need to pop twice to go back to the home page.
Action from the home page:
Home > add card page
- navigator.pop() takes me back to home page
Action from the details page:
home > card details page > add card page
- Having to pop twice here.
I know I have to do the pop
atleast once and then I used the popUntil
to check the current state of the route. For some reason, it is always popping twice.
After debugging, its showing current route as /
void _changeRoute(String newRouteName) {
bool isNewRoute = false;
// Pop once
Navigator.pop(context); // after this I should be on home page for the first path
Navigator.popUntil(context, (route) {
print('Current route is ${route.settings.name}');
if (route.settings.name == newRouteName) { // but this is showing current route as /
isNewRoute = true;
}
return true;
});
print('is home page: $isNewRoute');
if (!isNewRoute) {
Navigator.pop(context);
}
}
Upvotes: 0
Views: 192
Reputation: 473
I have this working.
My problem was that in my Main App, I had specified the home
property on MaterialApp
. According to the documentation, if the home
property is specified, it takes the route /
. So my home page is /
and I had it defined as /home
in my routes and this was being overridden by /
.
Upvotes: 1