I have an app with multiple pages where some pages are skip depending on the user selection...
for example, I have the following page
details.dart
contact.dart
address.dart
code.dart
summary.dart
I would like to go to another page from any of the above pages...However, I want first check to see if they are in the stack and then pop to it. Otherwise push that page onto the stack
How do I do this in Flutter
I think you might want to use Navigator.of(context).pushNamedAndRemoveUntil or Navigator.popUntil.
They are very similar however like the name suggests pushNamedAndRemoveUntil pushes a new route when the predicate matches, while popUntil re-uses an existing Route/Widget in the tree.
You can read more about them on this wonderful Medium post explaining all options in greater detail.
You can use routes inside materialApp, after home
return MaterialApp(
home:... ,
routes:{
"/routeName" : (context) => PageRoute()
}
and then call route name from any pages
Ex : app in Contact page need to move to PageRoute
//Contact Page
...
onTap:(){
Navigator.pushReplacementName(context,'/routeName');
}
just use multiple Navigator.pop
Related
n project I have a Home page(with list of movie data) with HomeBloc. On clicking the a single movie cell I navigate to movie details page which has MovieDetailsBloc. Now in the details page I can edit name or make movie as favourite etc, this needs to be reflected in Home page.
To update the Home page after a change in Movie details page, I am sending HomeBloc to Movie details page like this
MovieDetailsPage(homeBloc: <instance of HomeBloc>)
and in the MovieDetailsBloc I am calling the event of HomeBloc to update the Home page like this
homeBlocInstance.add(<home event>)
This method is working.
My question is Is this good method like passing bloc to a page? There will be many places from where this Home page may need to be updated. For example
Make changes in settings -> Update home page
In this case should I pass the Home bloc to settings page? But what if the navigation is like this Home page -> Summary page -> settings page, should I keep passing the HomeBloc from Home page to Summary page and then settings page? This does not seem right.
Am i doing it right?
or is there any better solution?
If you're using GoRouter, When you're navigate back to home, you can just pass param to the router state, so when you are go to homepage you can do some process.
For example :
context.goNamed(
'home',
queryParams:{
'somestate': 'somevalue',
'somesetting': 'settingValue',
},
);
I am using Getx for route management.
When coming from deepLink and the app is terminated, I want the app to launch (with splashscreen) and then go to a specific /match/:match_id route (I have the id in the deepLink).
This works well. The problem is that I want to push below this match page, the home page so that when the user goes back he can go there.
Within the initState of the LaunchScreen widget I am doing
Get.offNamed("/home"); // remove `LaunchScreen` and push `Home`
Get.offNamed("/match/:" + match_id); // push `Match`
This works. However there is some inconsistency with animations. I see for few seconds the Home page and then immediately the Match
I would like to jump directly to Match and the user not even seeing the Home one
I am going to build flutter app with Getx.
I followed nested navigator from
https://github.com/jonataslaw/getx/issues/799#issuecomment-730719165
at this example, only shows 1 step sub routes like "/browse".
I need to make another sub routes like "/browse/tabX" and "/browse/tabY"
when click browse button from BottomNavigationBar, It shows tabs (tabX and tabY) on the top. and tabX is default selected.
for the body, it shows automatically their pages.
Also using Get.Named('/browse/tabY') can access to open their page(select second tab and show their body).
Can you help me?
Oh Now I got the answer!
What I make a mistake is I did not use "sub-name".
for example, at initialRoute,
I used '/home/browser' but it should be '/browser' if it is sub-route.
Because the concept is route to subrouter's root and handle by subrouter.
so "initialRoute from root" route to /home first.
and "initialRoute from subroute" route to /browser again!
I use classed variable instead just string so I did not figure this difference out.
I have a typical CRUD operation task: (List Sites, Add Site, ...)
I created a SitesProvider. In the same provider, I added 2 methods for add and edit. I was thinking to use the same provider in the 2 screens (ListSites and AddEditSite)
In the ListSites screen, it works fine. Here is the problem:
I open the AddSite screen by clicking the AddButton in the ListSites screen
Hit submit (did not enter data, simulating error case)
The error gets displayed in the ListSites screen, not in the AddSite screen.
It makes sense. They both use the same provider, both screens are on the stack. It seems that the first one only consumes the state update and displays the error.
I use MultipleProviders approach that wraps the MaterialApp with all providers in the app.
Can we fix that without creating separate providers for each of the 2 screens?
EDIT:
I used provider.removeListener in the ListSites screen right before I open the AddEdit and it shows the error in the correct screen now. I still have to do some other tweaks to get it back to listen after I add. Not efficient I think but it is a step.
I ended up doing that:
Added Another Field in the provider (stateType: list/addedit)
Change the type per the screen I'm currently in
provider.stateType = UIStateType.add_edit;
await Navigator.push(context,
MaterialPageRoute(builder: (context) => AddEditSiteScreen()));
provider.stateType = UIStateType.list;
in build(), I check for the type
sitesProvider = Provider.of(context);
if (sitesProvider.stateType != UIStateType.add_edit) return Container();
I have two pages: page A has a "search" function, page B has a "select language" function. I navigate from page A to page B to select my language. Then I want to pop back from page B to page A with the new chosen language and perform the search automatically for that language.
I pass my chosen language from Page B to A by using SharedPrefs (which is working fine). So all I need is when I return to Page A, for the search function to get automatically triggered.
Is that possible in Flutter?
I tried using initState but that only works once when the widget is first built. I also tried didUpdateWidget but nothing happened.
Use this when navigating to PageB from PageA
Navigator.push(context, MaterialPageRoute(builder:(_) => PageB())).then((value) {
// you have come back to the pageA, now perform your logic
callThisMethod();
});