Flutter return a screen that has previous screens pushed into its navigator - flutter

I have the following situation:
I have an onboarding user flow that consists of lets say 5 screens where the user provides some personal details on each screen, the details are persisted into the database after each screen and then the user is taken into the home screen.
Desired behaviour:
When the user provides detalis until e.g. screen 3/5 and then exits the application, the flow should resume from screen 3/5, with the posibility to return back to screens 1 or 2 to change the already provided details.
I couldn’t find anything in the navigator documentation that would help me, and if you could provide at least a documentation lead for this edge case it would be amazing.
Basically I need to restore the stack of screens “in the background” when the user re enters the app and “show” the stack from the screen i/n (where i is the first screen where the user did not provide details in the last session) with the possibility to navigate back in the stack to the screens before i even if those screens were not shown to the user in the current session.
Or even more simply put, I need to restore a stack of screens in a new app session and show the user a screen from any position of the navigator stack based on data persisted on a db.
Thanks!

You can use PageView widget from flutter for your onboarding process, that way you can save on which page user was by the database entries, and you can directly jump on the specific page you want by the index. That way you don't have to worry about navigating through the screens.
Here's the link for PageView Widget implementation: https://www.geeksforgeeks.org/pageview-widget-in-flutter/

Well I think first you need to store the data the user already entered and the screen number he was on. You can use something simple as shared_preferences.
Then you have to use routes to navigate to a specific screen.

Related

Best architecture to propagate flutter state change from the bottom up?

I have a catalog (main page) that displays ads thumbnail, by taping on it, user is pushed a detail view from which it can tap on a message button which pushes the message screen. This message screen requires on some circonstances to be signed in: therefore it might display a button that pushes the sign in screen.
Overall here is the hierarchy:
catalogue -> ad detail view -> message screen -> sign in screen.
My use case is that when the user successfully signs in, I need to update the message screen.
I can propagate a 'onUpdate' callback down the tree but I am questioning that method.
Does Flutter offer a generic or better way to propagate state changes from the bottom up?
You can try
Using Global Variables for the State of the Button or
Using Provider
Personally for small Apps I use global Variables but as soon as it gets more complicated you should consider Provider.

Flutter Get Navigation - Removing route in the middle

I know there are some duplicate/similar questions, I read them all but I can't get the right answer.
I'm using Getx to navigate between screens(Get.toNamed()) and I was just wondering if there's any way to remove a certain route from the navigation stack. Let's say I have the following routing for my sample social networking app:
main screen -> post screen -> profile screen -> (another) post screen
I want to remove the first post screen and add a new post screen after the profile screen but able to go back to the profile screen when I press the back button from the second post screen(another back button goes to the main screen and if I go to profile screen(again) from the second post screen, the first profile screen will also be removed). The reason I want to do this is because I want the screens to have only one instance.
I tried saving the Get.rawRoute of post screen and called Get.removeRoute(route) when opening the second post screen from the profile screen but I get "'route._navigator == this': is not true." assertion.
Is it impossible because it's a stack? but.... I just don't think this is impossible, there must be a way...
Help me Flutter Masters!
Just kidding, saving the Get.rawRoute of the post screen and calling Get.removeRoute(route) before opening another post screen works just fine, I made a mistake. HAHAHAHAHAHAHA

The route.first page (MyHomePage) is updating even without a callback, function or notification listener

I can't really share code for this as it's for my entire ~large~ application, but is it normal for a flutter application's lower navigation stack to be updated when the current page is?
For example, in this page I have a standard form with a few TextFormFields:
Whenever I click on one to start typing the page sets state as expected, but by adding print("Update"); inside the build function of the bottom page of the navigation stack, I can see that page is being updated too. It happens on all pages I put on top of the first route page. I've also been experiencing that the home page gets slower as the app has been open for longer, could this be a cause for that problem too?

Why does Flutter show another page in the stack before showing the actual page on Back event

I have built a functioning flutter app using Navigator 2.0 API and RouterDelegate. All the navigation works fine but the problem is on the back event. On pressing back either from the app bar or from the device button, the top page does pop but the page that is 2 layers deep in the stack is displayed for a fraction of second before the desired page is displayed.
Like in the attached image, when going back from ArchivedChatThread, Bots is displayed for few milliseconds and then another page is shown. Here, the stack is also not proper as there should be one page between Bots and ArchivedChatThread i.e ArchivedChats.
I tried a lot of different things but am unable to find a solution as this is my first flutter app and Navigator 2.0 being a relatively new and complex concept.
image
Any kind of help is appreciated. Thanks!

Create new screen or retrieve existing screen?

When I am working on flutter navigator, I could not figure out what is the right way to create a screen.
For example, I have a Home screen and a Products screen. When app load, Home created, and then click a button there to go to Products screen, from Products I can go back to Home.
In this case, two screens are in the navigator stack. If I go to a third screen, and click the Products menu on the drawer list, it will create a new Products screen. Now there are two products screen in the stack.
My question is:
It seems it is normal to have multi instance of same screen in the stack, right?
Is it a good idea to always keep only one instance of one screen in the stack, and is it possible?
If we have to have multi instance of same screen in stack, isn't it wise to only load initial data once? for example, in this case, is it a good practice to keep the products list somewhere and don't pull data from server every time a new Products screen created?
Thanks
1, It seems it is normal to have multi instance of same screen in the stack, right?
No, it's not normal to have multi instance of same screen.
2, Is it a good idea to always keep only one instance of one screen in the stack, and is it possible?
Yes and it's possible with push and pop. Push is adding a new instance on the stack, pop will remove the top stack. It is a good practice to use pop when returning to previous page.
3, If we have to have multi instance of same screen in stack, isn't it wise to only load initial data once?
Depends on the data. If it is mutable throughout, then it is not wise to only load it once. If it is persistent throughout the app's lifetime, use Shared preferences plugin to store it. If it is a constant, storing it in a local database would be better.