How to update widget tree from another widget class - Flutter - flutter

I am facing problem to re-render the page when a variable changes in one class/widget (both parent and child widgets are stateful classes).
Here is how it goes:
I have three buttons in one class which changes a variable state (foodCategory).
int foodCategory = 0;
// down in the elevated button body - i am updating the variable
setState(() {
foodCategory = 1;});
While in the other widget, i am using this variable to perform certain actions:
for (var item in foodItems.values.elementAt(foodCategory))
GestureDetector(........ and so on...
However in the second snippet, the widget dose not know if there has been a change and it is not calling the buildcontext again...
I am not sure how to overcome this problem. I have tried valuelistenablebuilder but in vain. Maybe i dont know how to use it in the loop (i am using foodcategory as an int (iterator)).

it happned to be that i was sing valuelistenable builder in a wrong way.
It is easy. Just mark the variable and changes as valueNotifier. In my case, i needed to mark foodCategory as a valueNotifer.
Than, i needed to wrap the Widget (in my case column widget) as ValueListenableBuilder. This solved my issue.

Related

My ValueNotifier is not Working properly, why?

I have a GestureDetector inside a ListView and a CustomFilter widget in my Scaffold.
When I filter or change me showList in CustomFiter Widget it seems to be updated because I print the correct value from my CustomValueNotifier.
When it comes to my parent widget which is supposed to show my showList inside the GestureDetector the value is not updated at all and is at all times an empty list.
So the problem seems to be between the connection of my parent widget which hold both the filter widget, which upadtes the list, and my CustomValueNotifier, which takes the updated value but as I said before does not provide it at the widget.
Right now I also do the onnection with getter and setter so in the code im goin to show the showList will be taken the correct value from the getShowList() method. The problem is it doesnt change when the filter is being activated, on change of each filter, but after that when i press somewhere inside the gesturedetector again.
I have tried Change Notifier,Value Notifier, Provider does not seem to be what i can use in this example, setState but of course it updates on click in gestureDetector later

Flutter - How to avoid repeating StreamBuilder?

in my app I often need a list of objects that I get with 3 StreamBuilders. I want to avoid repeating those StreamBuilders every time.
So I tried to do a Stateful Widget that contains my 3 StreamBuilders with a Widget child argument returned by the last StreamBuilder.
But it doesn't work. The datas are not communicated to the child Widget.
What is the good way to do it please?
Click on StreamBuilder()
Then right click => select Refactor form the options => Click on Extract Flutter Widget
Now create a file with any name like streambuilder_helper.dart
Cut and paste the Extracted Widget in this file. Make sure to import material.dart
Now suppose you want to use this anywhere in your app just import the file streambuilder_helper.dart
While creating this flutter widget if you want to use this anywhere make sure to create several finals like
final myStream;
and then suppose the name of the flutter widget you defined is myStreamBuilder() then you have to create
myStreamBuilder({}); inside the flutter widget
and define this myStream final in it
myStreamBuilder({this.myStream});
and finally replace the hard coded code with your final in the StreamBuilder.

Flutter How to create a DropDownFormField after the previous DropDownFormField has data

I'm new to Flutter. What I want to ask is when I have a DropDownFormField and have 3 values. How can when I select one of the 3 values I will display a new DropDownFormField with a dataSource associated with the previously selected value.
For example I have House, Hotel, Apartment => Select House => display new DropDownFormField has Room, Kitchen
Or Select Hotel => display new DropDownFormField has Single Room, Double Room
Are there any examples that I can refer to?
There are a few ways to do what you're asking, but let me restate your query using Flutter nomenclature which should help you find more info. (DDFF = DropDownFormField)
You have two widgets, and when the first widget changes state you want to show the other widget.
The simplest way to do this is to create a custom Stateful widget that will contain two DDFF (in a Column(), Row(), etc). A widget's state is the values in its variables. In the case of this custom widget, you want to know at least if a value was selected for the first DDFF and what that value is so you can change things about the second widget. So there is one state variable: String ddff1Value;
When you build() your widget you will decide based on the state what to show. You can change what is returned from your widget's build() function using if() or ternary operators. If ddff1Value==null (nothing has been selected yet), don't show the second DDFF.
You also want to know when your first DDFF widget selection has changed, and DDFF provides a callback called onChanged that will be called when it changes. When it changes you want to update (set) the state of your widget to store that change, then redraw your widget reflecting the change.
To set the state of a Stateful widget, you call setState(). In your first DDFF:
onChanged: (val) {
setState(() {
ddff1Value = val;
}
}
When you call setState(), Flutter will automatically redraw your widget afterward. That's it.
Create your custom Stateful widget
In the build() method, decide what to draw based on state
Use onChanged() to get values when DDFF changes
Call setState() to update your widget's state and redraw

Flutter: hide/show widget by key dynamically

How can we show/hide widget inside another widget in flutter?
I have list of question which are inside list view builder I want to hide questions on user answer selection.
if you have all equations in question[] and whether to show them in show[], you could place into listbuild
return (bool[index] ? Card(child:Text(question[index])) : Container())
This returns the card in bool is true and the empty (not displayed container) otherwise.
(I think the other answer would do the trick too)
Another option you can consider though is using the Visibility widget to wrap the "question" widget and toggle the visible property based on a bool. To make this (or the above solution) work, you'd want to update the value of this bool using e.g. setState on the method called when,as you say, "user answer selection" happens.
Of course, if you have a more complicated application it might make more sense to use Provider for state management and wrap all of this in a Consumer.

what is the benefit behind provider or BLoC pattern?

We have a lot of State management solutions like providers and BLoC pattern. But, why do we need them?. why can't I create a file called 'data.dart' and import this file(data.dart) wherever i need, and make changes to the variables and objects in this file(data.dart)? Does this pattern has any downsides?
State management solutions are needed for datas that changes.
Imagine having data.dart file that has a variable
String text = 'abc';
And you have a Text() widget called TextA that takes in text variable as input.
This widget would initially display abc
now you have this function called
void changeText(){
text = 'cba';
}
how would this function tell TextA to rebuild because the value of text has already been changed?
of course you can use setState((){}); as long as the function is part of TextA,
but what if the Widget is ButtonA?
similar to StatefulWidget you can change the value using setState() to rebuild the widgets but what if you want a button in child widget that change the value in parent widget ? here you need to use provider to get full control.
Note: this is as example but it has multiple uses. you can use it with Firebase auth or cloud Firestore.