I have a PageView( ) displaying four different pages and a BottomNavigationBar used to navigate to those pages. I have a FloatingActionButton on the first page when I use Navigator.push() to push the second route and Navigator.pop to pop it out the initial page rebuilds again, The same thing happens when I use it on the fourth page too, The first screen is returned instead of the same screen as I'm performing an operation on that page. So how do I navigate back to the same page while preserving sate of that page?
I have used AutomaticKeepAliveClient mixin to preserve the state of the pages while navigating through the bottom navigation bar but not able to do so using Navigator.
This article by Andrea Bezzito does cover some parts of it but I wasn't sure if that is exactly what I wanted. so wanted to look for alternative solutions if any available.
Thank you.
What you're looking for is called Provider
. Here is a very simple example of how you could use provider. https://github.com/m-Skolnick/provider_example_flutter
hey @mskolnick ! thanks for responding. I have used the provider in my application, and I'm using it in many places to provide values as well as use Changenotifier to maintain ListView items for my ListView.builder(), But I'm not sure what point I'm missing. Should I be using Provider to display my pages through my PageView widget too? Is that where I'm going wrong?
Ah gotcha. I may have mis-interpreted your question. I was thinking you were having trouble preserving the state of your page when returning to it. That is where Provider can help. As far as having a separate navigation stack for each of your tabs, the article you linked to seems like a great solution.
Its a mix of both, I'm having trouble preserving the state while returning through Navigator, which causes my screens to rebuild, which is useless rebuild as it'll fetch those documents again from firestore. I tried using IndexedStack instead of PageView, it works in a sample app but not in my app, as sson as I Navigate using Navigator its lost. State is preserved only in the BottomNavigationbar
It sounds like you're not saving the state of your page. Ie.. The data will always be cleared when stateless widgets are rebuilt unless you are pulling the state from a stateful object. If you want to preserve the state of your pages in between rebuilds take a look at that provider repo that I linked to.
Thanks for your time, but I found my bug, I have a Future builder which shows a form or the home screen depending on the fact that if the user is already registered or not so that Futurebuilder was rebuilding my widget Tree every time cause it was checking if the user was present or not. So I take out the FutureBuidler and now the unnecessary rebuild is saved and my state is preserved .