how to make changes from another screen in flutter - flutter

I'm new to Flutter and I need help with logic part for my project so if this question doesn't belong in SOF, please let me know.
So I have stacks of cards in one screen and I want to control those cards from another screen but I'm not really sure how to implement and the logic behind it so can somebody help me?.
so the idea is, The user can view stacks of cards in Home Screen but they can't reorder their position, if the user want to do that than he have to go to Reorder Screen.
Basic idea of the app will look like
Home Screen
.......................
Card 1
Card 2
Card 3
//All cards are stack together
Reorder Screen.
.........................
Button 1 (syn to Card 1)
Button 2 (syn to Card 2)
Button 3 (syn to Card 3)
it will appear in this order but the user can move their position
by drag and drop and this will also change the order of the Cards in Home Screen too,
I understand that It'll be very difficult without looking any code but please let me know how you going approach to implement this feature if you were asked to do it.
Any suggestion or help on the logic part will be really appreciated. Thanks

What you are trying to do is called state management. You state is the data which can change. The simplest option is to pass variables from the card screen to the button screen. The button screen would then make changes to this state, and pass it back. You can do this with just stateful widgets and setState. For more complicated state, you will need to use a more advanced system like the bloc pattern or providers.
For this example, you could pass the list to the list to the button screen from the card screen. The button screen modifies this list, then you use setState to apply changes to the screen. Be aware of widget keys when changing the order of items in a list.

I think that what you want is a state management solution. There is a lot of them, with each their pros and cons. I (and the Flutter team) would suggest Provider.
Basically, your Provider would hold the cards data list and a function to reorder it.
Then, both the screens would depend on that data (using a Consumer).
And the reorder screen would call something like context.read<YourProvider>().reorder(oldIndex, newIndex) to reorder the list.
Take a look at this for further details : Simple app state management
If you want to get informed about other SM solutions : List of state management approaches

Related

How interactive can a component be inside of a widget?

my job is to create a few widgets using widgetkit and swiftui, however the main project is build on uikit so I am not very good with swiftUI. Nevertheless, I would like to know whether I can add a graph inside of, for example, medium sized widget, and can the graph be scrollable to reveal more information? Or could it only be clickable? Thank you.
I know how widgets work, just interested in whether it is possible to create a scrollable graph inside of a medium sized widget.
I'm only just getting started with widgets myself, so happy to be corrected. But interactivity with widgets is very limited - effectively to taps and nothing else. They're designed more to be informational displays than interactive user interfaces.
If your widget is larger than the small size (the square one that takes up the same space as 2x2 app icons on the iPhone home screen), you can have multiple tap targets. For example, if your widget had a list of calendar items, you could set it up so that a user tapping on a specific item opened the app with a view showing that item in more detail.
So no, a scrollable graph isn't feasible. But what you can do is create a graph that shows a snapshot of data that a user is most likely to find useful when glancing at their home screen – and making sure that if they tap on it, they go straight through to a more interactive version of the same data.

"Minimizing" the current screen in Flutter

I'm building an app where I ping some data in my screen with the StreamBuilder (time, location, etc.), now I want to make an option for the user to leave the current screen without breaking the state or context of the screen, sort of like minimizing the screen. How can I achieve this in Flutter? Example:
I have my Screen One where I ping the data:
Now I want to make an option to the user to check some other data in the back while the Screen One is still running in the background so it doesn't break the ping of data from the StreamBuilder:
If I use the Navigator.of(context).pop() that will naturally pop the current screen and return to the previous one, so what I'm looking for is a solution not to break the state or context and still be able to return to the screen, like I said, sort of minimizing the screen.
Any idea of solution is quite helpful, thanks in advance!

Preserving the State of a Drawer in Flutter

I have a stateful Scaffold widget, where the body contains a list of entities with various properties (e.g. shoes or cars) and Drawer widget, which is empty at first, except for two buttons at the bottom ("Cancel" and "Filter") and a FAB. The user can add various filters with the FAB (e.g. the user can add a shoesize-filter, or a color-filter).
The problem I have is the following: Let's say the user selects various filters and works with them (i.e. ticks checkboxes, enters a shoesize, changes sliders, etc.). When the user is ready to apply the filters, he can click the "Filter" button, which closes the Drawer (onTap -> callback -> Navigator.of(context).pop()). But when the user wants to go back, reopen the Drawer, and adjust one of the filters, it obviously doesn't work, since the widget is being rebuilt from scratch.
Currently, the way the filtering works is, once the "Filter" Button is pressed, all the various values from the added filters are collected into an object FilterPackage, which is then passed via callback to the Scaffold widget, which then applies the values from the FilterPackage to the list of entities.
One solution I came up with, would be feeding this FilterPackage object to the Drawer widget in its constructor, which would provide all the information necessary to rebuild the widget just how it has to be.
Is that a reasonable solution? I have already made some research, but struggled finding a solution on what would clearly be the best and recommended way to preserve the drawer's state. Using PageStorage / PageStorageBucket seems overly complicated for this case. Or am I wrong?
Thanks in advance for any guidance.

How to load data on a screen on the press of a button without routing and rebuilding whole screen

Pardon me if this is a naive question but I am trying to figure out a way in Flutter to load different data when the user clicks a button. The way I currently see is routing the user to a new screen everytime but I am sure there would be a better approach without loading the whole screen everytime
You should use a Stateful Widget
I would create a Future Builder / ListView Builder. Depending on how you want it to behave you could have the ListView items clear before regenerating the list of items to show.
Future Builder you could create a function to return certain widgets depending on the request you make with the function.

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.