Reputation: 103
I want to update a screen on coming back to that screen using Navigator.pop
.
E.g. I have Home
Screen and Some steps screens (Step1
, Step2
, Step3
, Step4
)
Home screen is a stateful widget and shows number of steps completed.
The flow is like
Home => Step1 => Step2 => Step3 => Step4
On any step, on pressing back or pressing close button, it should go back to Home
screen and home screen should show updated state of steps completed. So i am navigating to next steps in steps screen using Navigator.popAndPushNamed
so that Navigator.pop
will go to Home
screen.
And using Navigator.pushNamed(context, 'Step1').then
in Home
screen to update it.
But the problem is .then
is called just on Step1
itself because it got popped and if user goes like
Home => Step1 => Step2 => Step3 => Home (using pop)
Home scren will only show Step1 is completed because when going from Step1 to Step2
, its .then
callback was called and when coming to Home
from Step3
using Navigator.pop
, no code is being executed and i am unable to update the Home Screen.
I know i can use Navigator.pushNamed(context, 'Home')
from steps to go to home screen but it doesn't looks ok (it looks like Home screen was pushed on it instead animation should be like it goes back).
Upvotes: 1
Views: 848
Reputation: 103
found the solution using RouteObserver
and implemented something like example given in https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
The method i was looking for didPopNext
of RouteAware.
Upvotes: 1
Reputation: 3469
You can use a variable in your previous page which will keep a track, if you are coming from the next page and return a boolean true variable, stating that it has returned from the previous activity. You can use that varible in your init method and if the value of that variable is true you can just fire an event which will return appropriate state.
Upvotes: 1
Reputation: 1447
Check this, It's Working For Me!
Navigator.of(context).push(route).then((val){ //you can update you data here, when coming back })
Upvotes: 0