Same bloc functions in different screens - flutter

I'm having a serious issue with code duplication because I have created 2 different instances from a Bloc that holds the color change of a button and provided them to 2 screens. I wanted them to act independently, but unfortunately, both buttons work the same. Can anyone give me a clue to achieve this without creating another bloc for the next screen?. I have provided the bloc to the main screen and on the 2nd screen, I have bottom navigation and it has 2 screens.

You can define a class yourBlock with your functions ecc then in your pages you can include that custom block and use it as you want.. so if you need to add, modify or to do anything on your code you have to do only once in your custom class

Related

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

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.

Does MVVM require to remove all logic from the Widgets?

I'm having confusion regarding the correct implementation of MVVM architecture in a flutter.
Does MVVM require separating just the UI calls from the UI or
separating the complete logic of the widgets, even if it is to update
a CheckBox?
If using setState does not raise performance issues, should we use it when following MVVM architecture?
If it is okay to have some logic inside of widgets, to what extent
should we do that?
Yes obviously , I suggest you to study Clean Architecture, Basically concept is that, You have to create ViewModel for every screen.
Screen will contain all the Widgets (View)
View Model will contain all functionality and variable which will be hold value which used in Screen (View)
It is perfectly okay for the widget to manage its state internally. It wouldn't make sense e.g. ListView not to manage its scrolling state. The same obviously applies to custom widgets.
See Differentiate between ephemeral state and app state for more information.

In Flutter is there a way to have different navigation stacks

I come from react-native and I am used to using react navigation, which allows you to make different stacks each with different routes in them.
Flutter appears to work where you have to put all your routes in one file, is there no way to split them up?
My app has 3 main sections and each screen in a section would ideally share blocs, but I cant find a way to make each section independent of the other sections which means either all the screens must share all the blocs or none of them.
Maybe you want every screen in section possible have a Navigator of itself ?
Default Flutter using a Navigator in app and navigate with Navigator.of(...). An app can use more than one Navigator with Navigator Widget.
Nesting Navigators
Code sample

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.