1

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?

0xba1
  • 76
  • 1
  • 7

1 Answers1

0

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.

Adel B-Lahlouh
  • 1,059
  • 1
  • 8
  • 20
  • Hi, already know this (maybe not much of a newbie anymore). What I'm trying to do is setState from a very separate widget in the same page. – 0xba1 Jan 24 '21 at 12:44
  • possible duplicate of: https://stackoverflow.com/questions/50430273/how-to-set-state-from-another-widget ? – James Jan 24 '21 at 12:54
  • @Boluwatifẹ Can you attach the code to help you better? – Adel B-Lahlouh Jan 24 '21 at 13:01