Below is my app widget tree. If user is not logged in then login page is shown if user is logged in then work page. on account page I have logout button.Logout button implements push replacement and shows login page.
Issue is - When user click on back button again work page is shown.
How to remove all the routes from widget tree and only show login page after logout action?
Note - I am not using Named routes, just Push , Pop and Replacement
If you are using namedRoutes, you can do this by simply :
Navigator.pushNamedAndRemoveUntil(context, "/login", (Route<dynamic> route) => false);
Where "/login" is the route you want to push on the route stack.
Note That :
This statement removes all the routes in the stack and makes the pushed one the root.
Close your dialog by calling,
Navigator.pop(context);
and then call pushReplacement
void _doOpenPage() {
navigator.pushReplacement(
MaterialPageRoute(builder: (BuildContext context) => MyHomePage()));
}
pushReplacement Replace the current route of the navigator by pushing the given route and then disposing the previous route once the new route has finished animating in.
Read More here
Related
So I want a user to be able to go back to the '/' named route after he/she reaches upto a certain page. The condition is that when that page is reached, user should be routed to '/' only on Navigator.pop(context).
ie., Lets say I have routes configured such that :
User is at '/' > Clicks a button and pushNamed to '/abc' > Clicks a button and pushNamed to '/xyz' > Clicks a button and pushNamed to '/mnq'
Now, I want that when user is at /mnq and Navigator.pop(context) is executed, user should be sent back to '/' and not anywhere else.
When I'm at /xyz, I tried to do a Navigator.pushNamedAndRemoveUntil(context, '/mnq', ModalRoute.withName('/'), arguments:....) but It does not work because when Navigator.pop is executed after reaching /mnq, I see a black screen.
Is there a way to do this. Please note that I need to send arguments from /xyz to /mnq.
You can try use pushReplacementNamed()
'/' pushNamed to '/abc' pushReplacementNamed to '/xyz' pushReplacementNamed to '/mnq'
read this article for more information
pushNamedAndRemoveUntil is remove all page before. when you use back/pop, it will show black because no page before. you can pass argument with pushReplacementNamed Flutter - Pass values between routes using pushReplacementNamed or store data in local https://pub.dev/packages/shared_preferences
1st. on pressing a button to user should be sent back to '/'
simply use Navigator.popUntil(context, ModalRoute.withName('/'));
in place of Navigator.pop(context);
2nd. if user tab back button
WillPopScope(
onWillPop: () async {
Navigator.popUntil(context, ModalRoute.withName('/'));
return false;
},
child: Scaffold(......),
)
I'm using flutter navigator 2.0 in my app and I'm trying to return some data from another screen when it pops. Here's the code for the same:
This is what I'm using to push/add a new page. After the user has made changes to the edit profile page, I'm trying to update the data on the existing page (profile page).
appState.currentAction = PageAction(
state: PageState.addPage,
widget: EditProfile(currentUserId: currentUserId),
page: Edit_Profile_PageConfig,
);
After the changes are made on the edit profile page, this is what I'm doing to pop:
appState.currentAction = PageAction(state: PageState.pop);
But I'm not sure how can I pass the data from the edit profile page back to the profile page. In the old navigator style, it was pretty easy to have a then function, like this:
var callBack = await Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => EditProfile( )));
But I'm not sure how to achieve this using Navigator 2.0. I referred to these two references for developing the navigation in the app.
Flutter Navigator 2.0
Google Docs
when calling navigator.pop add your data as a second argument.
Navigator.pop(context, data);
I am new to Flutter.
I have three classes, the first one is the Home Screen and the second one is from the SDK that is used to call the API through a function in the third class.
I want to go to the second screen from the Home Screen and there I have to call the API function from the third class.
After calling the API, I have to come back to the second screen where I am waiting for the response. I have to pass this response to the Home Screen.
After calling the API function, I will take the user to the Home Screen immediately.
I have to get the response at the Home Screen after the API will return it.
ApiMethods is in the third class and apiRequest is its function that is calling and returning the response of the API.
ApiMethods().apiRequest().then((response) {
//controller will come here after getting response
//here should be a callback that will take response to homeScreen
});
//going immediately back to home screen after calling API
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => userHome),
(route) => false);
You can use Navigator.popAndPushNamed(context, userHome).
How can I navigate to an inner page of a PageView when I'm on another page. For example: say I have the following pages:
Home
All Messages > Single Message Page
Contact
When I'm on the Contact page, how can I go straight to the Single Message Page? I feel it's easiest to use named routing, so would love to have sucha sollution....
As you mentioned in your own question, named routing would be the right way to do so.
With named routing, the route tree is much easier to understand and move around.
For Example, I defined in my main app widget, the exact routes in my app.
As you can see from the Flutter documentation:
MaterialApp(
// Start the app with the "/" named route. In this case, the app starts
// on the FirstScreen widget.
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => FirstScreen(),
// When navigating to the "/second" route, build the SecondScreen widget.
'/second': (context) => SecondScreen(),
},
);
After you have defined the routes, you can move to this route by pushing a named route to the Navigator of your app.
// Within the `FirstScreen` widget
onPressed: () {
// Navigate to the second screen using a named route.
Navigator.pushNamed(context, '/second');
}
Read more at https://flutter.dev/docs/cookbook/navigation/named-routes
Wish you luck!
I have a page which is calling multiple classes and different widgets
and by clicking on it user can hop to any page.
Currently the issue is I want to refresh the original page everytime it is shown to user.
When it is freshly loaded (causing no issue for now).
When user comes back to it from some other page using back key.
old data is shown on the page, which I am trying to get rid of.
I have tried using Navigator.push method (Observer class method) and tried to listen when user presses backkey.
but there are multiple pages serving unique requests and I don't want to link everyone with that first page.
as while clicking Navigator.pop(context) method i'll have to pass some string.
Does any one know
how can I refresh the page when users comes back to it using backkey.
Navigator.push returns a Future when the MaterialPageRoute passed into it is resolved. You could await that Future to know when some other view has popped back to it.
Example,
Navigator.of(context).push(MaterialPageRoute(
builder:
(context) => NewView(),
),
).then((_) {
// Call setState() here or handle this appropriately
});
Had the same issue, the following worked for me (below code is in FirstPage()):
Navigator.push( context, MaterialPageRoute( builder: (context) => SecondPage()), ).then((value) => setState(() {}));
After you pop back from SecondPage() to FirstPage() the "then" statement will run and refresh the page.
NavigatorObserver can listen to route changes.
You need to create your implementation overriding didPop, didPush etc.
Then you can pass it to navigatorObservers field of MaterialApp or Navigator