I have problems with flutter state management

Viewed 87

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?

1 Answers

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