flutter Child1 send data1 to Parent which forwards data1 to Child2 - flutter

I have a Form widget with multiple TextFormFields.
As flutter is not that readable I am putting even simple form fields each in its own widget.
No idea wether this is bad...
So I wrapped a TextFormField and a Radio Button group each in its own statefull widget.
Now both can NOT communicate anymore together.
When I change a value on the Radio button widget the Text Form field widget should change its value too.
How can I do that?
Actually I have a parent with 2 children and they need to share data.
I havent found a communiction strategy in flutter for that advanced use case... working with 2 functions here seem really a bad and cumbersome idea...
In angular I used a shared singleton service for such scenarios. Exists there something similar in flutter?

Yes you can use singletons with get_it package https://pub.dev/packages/get_it or use provider to send data between pages

Related

How to validate TextFormFields, that are at different pages inside a PageView

So lets say I have 3 Pages : [StartingPage, NamePage, AdressPage] inside a PageView.
Each of those children has a TextFormField inside it.
When I am at StartingPage I want to call .validate() on the keys of the TextFormFields on NamePage and AdressPage and I want the errorLabels to show, when the user navigates to the other pages. The problem is that currently, the validation only works for the TextFormFields that are on screen.
So basically there are two questions:
Is it possible to pre-build pages of a pageview? Otherwise when I call formFieldKey.currentState the state is null if I haven't navigated to the respective page.
Is there another way to validate offscreen TextFormField content and show the errorlabel, when they come into view?
Unfortunately Flutter Dosen't Support Multi-Page Forms So You Need To Implement Advanced State Management To Make This Happen.
I Used Riverpod To Create a 4 Page Form By Creating A Form Key For Every Page And Saving The State of the Forms, This Way Every Form Will keep Its State Until You Dispose Of it, Hope This Helped.

Communication between Flutter widgets

Flutter widget update pattern is a little bit confusing when coming from "old/classic" win32.
For instance :
I have a widget "button" => I click on it => I update a cell in a widget "datatable".
With classic API (VCL, Net Forms, ...) I get the address of button (by name, id, ...) and call directly
datatable_address.cell[x,y] = new_value;
I understand that I have to use setState() but do I need to create an "event" in Datatable (to run its setState()) and fire up this event from my button ?
(BloC seems pretty close to Qt signals)
The difference is composition vs aggregation. The widgets themselves are immutable so you can't change the value inside a widget.
What you can do is create a new object with latest values. Flutter framework will take care of the rest. For example,
when you do setState in parent widget of both button as well as datatable, you build method of said widget is called. Now, you will create a new datatable object with updated cell values. Internally, flutter will handle this.
You can only do setState in State class which is not immutable which is linked to Stateful widget which themselves are immutable.
This architecture comes with its own set of problems like if you need to update some UI based on some button press, you will have to have the logic inside the common widget of both the affected widget and button. That's where Stream/Bloc comes into picture.

Flutter nested lists - custom CheckBox

I'm really confused about this situation, I'm trying to build this app just like in the image below, the app is all about having a list of ExpansionTiles inside each one there is a list of items, each item has it's own three checkboxes[true, false, avoid], and also the ExpansionTile it self has it's own three checkboxes that can be clicked globally, instead of selecting them one by one,what I need is to get to the best solution to implement this, to know which item in which list were selected and to give a result depending on it, I'm really confused and tried many ways, many models for data, but failed, any help will be really app, I'd be more than glad with any help.enter image description here
I'd create a controlled widget for the three checkboxes, passing parameters for onChange and value (Value can be determined using an enum)
Then I'd reuse that widget everywhere I need
Then I'd go for an expansible widget that contains the list (Column widget maybe?)
Try to handle as many states as possible, having controlled widgets
Or if you're using BloC you can easily implement your core logic with this UI:)

Flutter - Call a method on some children from parent

I'm discovering Flutter and I really love it.
I'm building a simple app where I have few instances of Statefull custom widget "Counter" on a Scafold's body, and I have a "Reset" button on the drawer.
What's the best way to call a "resetCounter" method on all "Counter" instances when tap on the "Reset" button ?
I've managed the other way (calling a callback method of a parent from a child), but I can't find the other way. The only solution I've found is by using GlobalKeys, but it doesn't seems appropriate.
Thanks
Indeed for your case callback function might be the right one. But when your app will grow the widget hierarchy will become more and more complex. For now you have Parent —> Child relationship and it’s quite easy to pass callback function, but when you’ll get Parent —> Widget A—> Widget B—> Child situation in order to pass callback from Parent to Child you’ll need to pass it through Widget A and Widget B as well.
For me, your problem sounds like a perfect candidate for reactive streams. You may create a Sink which will be used when user tap on Reset button and your Counter will observe this event and handle it with resetCounter method. You may learn more about reactive programming (and other ways to solve your problem) from this presentation.
There is another approach to extend ChangeNotifier, and add child as listeners. Once parent perform an action, you can call notifyChange to trigger child widgets callbacks.

Inter Widget communication

Is it possible to do inter widget communication via something like a notification/event bus?
I need to be able to tell one widget to react to something that happened in another and didn't want to create a hard link.
The notification listener will only fire is if it is higher in the widget tree than both of the widgets so that isn't probably a viable solution.
There are lots of ways to do this depending on your use case.
You could have them be AnimatedWidgets that are passed a ValueNotifier or ChangeNotifier as the listenable. You can see this pattern in the Gallery's animation example.
You could use StreamBuilder to have your widgets rebuild automatically when new events come in on a Stream. There aren't a lot of examples of this in the main Flutter repo, but it's something that you're likely to need once you start using plugins or doing network I/O.
You could use a GlobalKey to get currentState and have one State call methods on the other. This is how snackbars work (example).
You can also extend InheritedWidget to provide widgets with information that wasn't passed as a constructor argument, and they'll automatically be marked for rebuild when that information changes. This is how Themes work, for example.
If you can provide more details on what your widgets do / what their relationship is, or ideally a code snippet I can help you decide which approach would make the most sense for your situation.