I have problems with flutter state management [duplicate] - flutter

This question already has answers here:
How to set state from another widget?
(2 answers)
Closed 2 years ago.
send help.
I'm a newbie flutter developer who's trying to build a simple calculator, but I can't figure out how to affect a widget's data from another widget (say a button). I'm trying to change the state of one widget from a very separate widget using buttons. How do I do this?

First you have to know the difference between StatelessWidget and StatefulWidget.
StatelessWidget : are those in which you want to make a UI that does not need to be dynamically changed whenever you update any value bound to it. For example, if you want to make a button whose title doesn't need to change dynamically, then you can create a separate widget for a button as a Stateless widget.
StatefulWidget : are just the reverse of Stateless widgets. This means when you want to make something that you want to change dynamically according to how a user interacts with it, then you can use the Stateful widget.
For example, if you want to change the background color of the app on click of a button, you can make use of Stateful widget in this case.
So if the state of the widget changes you have to call setState to trigger a rebuild of the view and see immediately the changes implied by the new state.
setState(() {
value = newValue ;
});
To know the topic in more detail, you can check this.

Related

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.

What is the difference between a data type widget and a stateless widget in Flutter?

I'm quite new to flutter and I'm trying to figure out the best way to build my widgets in the aspect of good performance while I have three choices:
1- Stateful Wigdet.
2- Stateless Widget.
3- Widget function(){} with a StatefulBuilder if needed.
Thanks.
A StatefulWidget has State, which means that you can change stuff inside the widget without needing to destroy it and create a new version. They work slower than StatelessWidget but are better for checkboxes or screens which need to be frequently updated.
A StatelessWidget is locked, and you cannot change values once it is initiated. In order to change things inside the widget, you need to re-call the build function to build an updated version. They are quite fast but are best used for static screens.
Widget function(){} widgets are custom-made. Essentially, you can take any existing widget and modify it, adding childs and other widgets to it. However, the widget type normally inherits from a StatefulWidget or StatelessWidget. These can cover basically everything.

Make Stateful Widget inside a Stateless Class

Is it possible to make a Text Widget Stateful so that the Text can be changed when i press a Button without having my Class to be Stateful?
example:
class Rangliste extends StatelessWidget{.... Text(Test), Text(Test2)....}
Test and Test2 are Strings.
So when i press a Button the values inside the Text Widgets should change. Is there any Way to achieve this without changing my Class to Stateful? Or if not can somebody explain how i can change the Class to Stateful without messing up the rest of my Code
No, you can't, a stateless widget is an immutable widget that cannot react to state changes and rerender, you will have to use some form of state management.
If your button and your text and your data are all within the same widget, then it is better to make your widget Stateful and use setState(() {}) to change your text and rerender.
You can use the Flutter plugin in order to convert your widget from a Stateless widget to a StatefulWidget, the plugin will handle everything.

check if a widget is on screen

Is there a way to detect if a widget is on screen/seen anywhere on the app. For example in a TabBar or a PageView.
I already know that I could use the widget's build method to detect this, but this results in some really weird behavior where sometimes the widget has already been built and when the user navigates to the screen nothing happens.
So is there any way to do this with an entire app?
Maybe you can try using widget key like a Global Key
final key = GlobalKey();
and they passes it to the widget you want to know the state

What difference between stateless and stateful widgets?

I am learning Dart/flutter and trying to understand how Widgets system works. But I can't understand what difference between stateless and stateful widgets? For example I have button. What type it have?
Let's imagine two cases.
I send text to button and it's display it.
I send text to button and it's change color.
What will be if I will create not proper Widget type?
There are 3 kind of widgets, not just 2.
Stateful widget
Stateless widget
Inherited widget
A stateless widget is like a constant. It is immutable. If you want to change what is displayed by a stateless widget, you'll have to create a new one.
Stateful widgets are the opposite. They are alive and can interact with the user. Stateful widgets have access to a method named setState, which basically says to the framework "Hello, I want to display something else. Can you redraw me please ?".
Finally, Inherited widget is a mixt of both worlds. It is immutable and stateless. But another widget (whatever it is) can subscribe to that inherited widget.
Which means that when you replace your inherited widget by a new one, all the widgets that has subscribed to the old one will be redrawn.
In the end, a stateful widget will usually be used like a Controller.
A stateless widget will be used like a View.
And the inherited widget will be your configuration file or your Model.
According to flutter.io:
Stateless widget
Stateless widgets are immutable, meaning that their properties can’t
change — all values are final.
Here is doc.
Stateful widget
Stateful widgets maintain state that might change during the lifetime
of the widget. Implementing a stateful widget requires at least two
classes: 1) a StatefulWidget class that creates an instance of 2) a
State class. The StatefulWidget class is, itself, immutable, but the
State class persists over the lifetime of the widget.
As a example if you want to change text in text widget when a button press, you have to use StatefulWidget and it will let you to change the state of a variable.
But in StatelessWidget you cannot do that because it doesn't keep state.
Read more from doc.
This tutorial will help anyone who trying to understand these two.
Check out the Flutter Interactivity Tutorial.
If your widget's build method depends entirely on its immutable constructor arguments, you should use a StatelessWidget because they're simpler. If you want to store some persistent private data that you expect to mutate over time, use a StatefulWidget and store the data on the State.