Flutter - How to preserve widgets with data and states, when navigating using the NavigationBar class - flutter

I'm using the NavigationBar class for the navigation through my app, but I have a problem, which is that I can't preserve the information on the screen when I'm navigating, so everything loads up again at the start of the screen. How should I do to preserve the screen even if I am in another screen so everything doesnt load up again?

You can use bloc for your state management.

Use states. There are a lot of options available like for example stateful widgets.

Related

is it better to use Navigator over BottomNavigationBar when we talk about performance in Flutter?

Is it better to use Navigator over BottomNavigationBar when we talk about performance? like is this "bottom navigation bar" not gonna demand more resources, since, in Navigator.pop, the route will removed from the memory, but with the BottomNavigationBar all routes will be stored in the memory and just display the index we want? am I correct?
BottomNavigationBar just executes a function when you tap on a tab—how the tabs are implemented is up to you.
With an IndexedStack, every child is built when shown, and visibility is simply toggled when the index changes.
You can also use AutomaticKeepAliveClientMixin if you want to preserve a State when changing tabs, without building them all at once.
If you want the tabs to be disposed, you don't need to use either of these. Simply build the body of the page depending on the index of the BottomNavigationBar.

Flutter: Using Hero without Navigation

I have an app that uses a BottomNavigationBar to switch between two screens with their own navigation stack. I would like to use the Hero tag to create a custom transition between the two, but since I don't use navigation, but rather change the state to switch between screens, I'm not sure if this is possible at all.
Is there any way to recreate the transitions provided by Hero without using Navigator?
The Hero widget is not what you are looking for. Try the animations package made by the flutter team. Here the link to this package.
Is not easy to use, but get a try, or you can use the default Navigation built in.

Navigator pushReplacementNamed loads screen twice in Flutter app

Inside Flutter app I have MaterialApp with list of named routes and bottom navigation bar with few tabs. On one of tabs I have button with this code on tap
Navigator.of(context).pushReplacementNamed(MyRouteName);
For some reason target screen is loaded few times (it can be seen on screen) and it's initState() method is also called at least twice. Why is it happening and what can be done with it?
I've had the same problem too, I solved it by going back with Navigator.of (context).pop Actually, the solution may vary depending on your status as the route. I suggest you try alternative methods.

How to distribute state of BottomNavigationBar across the app in Flutter?

In my app, the navigation bar works fine between the tabs, but when I go inside of each item in my list, I want the navigation bar to be there too.
What I did is that I made it a separate widget, and called in the bottomNavigationBar inside the Scaffold of the pages, but the problem is that I am changing the state of the navigation bar in my main page only, so the state doesn't get distributed across all pages in my app.
How do I make the body of the page change in an app where there is not only one body, but many?
Thanks!
Since there is not code over here in your post and I don't know what you have done so far so I can only advise you to go through this post.
I know you might have already seen this, but still, try to follow and this will really help.
Thanks....!!!!!!
https://medium.com/#lucassaltoncardinali/keeping-state-with-the-bottom-navigation-bar-in-flutter-69e4168878e1

Determining if widget is in the foreground/background

Say I have a Flutter app with two screens - screen A and screen B. Screen A is just a ListView that displays a list of items but needs to perform a network call whenever the list changes and then update the list. Screen B can modify the list of items but I want screen A to only perform the network call when screen A is in the foreground and being displayed. As far as I can tell, I cannot do this easily. Screen A is not disposed and reinitialized when navigating to and from screen B. It would very helpful if StatefulWidget had onForeground and onBackground methods to override, but they do not. This problem is not exclusive to navigation either, but this same problem presents itself when using PageView with full-screen pages. Is there some proper/standard way of implementing this?
My current setup is as follows: I let the parent widget to both screen A and B hold the list in a ValueNotifier and pass it to both screens when constructing them. Then, screen A listens for changes on the ValueNotifier and performs a network call whenever it does. So, in order to determine whether screen A is in the foreground/background, I will have to start/stop listening for changes before/after navigating. But I haven't started implementing this, as I think it will get complicated when widgets far down the widget tree trigger the navigation or other widgets need to know whether they're in the foreground/background.
Another option I've thought of is instead of observing for changes in the list, I could rather just return a result from screen B saying whether or not the list changed and react then. But I can think of many ways this can complicate my code as well since my real app involves more than just one dependency. I would have to create a custom result class for each screen containing a record of all the data that changed then it would be tedious if I want to add more data in the future. And how would I handle navigation to screen B then screen C? The result would have to be retained and passed down so screen A can react to changes made by screen C. I would also have to ensure all calls to Navigator.pop contained the result, and override back button presses to pop with the result. And I'd also have to ensure that the result makes it to the proper widgets that need to react to changes.
Am I just missing something simple to accomplish this? I am not the most experienced with Flutter and I wouldn't be surprised if there's some easy solution I haven't learned yet.
Edit: After some more testing, it appears AnimationController does something similar to what I need with the vsync parameter, in that it does not update when the State is in the background or when it is not being drawn. So, I could use SingleTickerProviderStateMixin on screen A's state, then use createTicker to create a Ticker, and then check if the Ticker.isTicking whenever the list changes. If it is ticking, screen A is in the foreground, otherwise it is in the background. Although I'm not sure if this is a good approach, since it appears Ticker's are really only used for Animations and there's nothing documented for a use case like mine.