I am trying to have a sequence of events happen on button press:
- User clicks Start button
- Button text changes to "Stop"
- A calculation happens and then updates another widget
But it seems like the onPressed function only allows one setState() to update the view, because the problem is that the button text never changes.
Here is the button widget:
new RaisedButton(
child: Text(_calculating ? "Stop" : "Start"),
onPressed: () {
if (_calculating) {
_setCalculating(false);
// stop calculating
} else {
_setCalculating(true);
_doCalc();
}
},
),
Here are the callbacks:
void _doCalc() {
setState(() {
sleep(const Duration(seconds:1));
// Do some calculation and display the result ...
_setCalculating(false);
});
}
void _setCalculating(bool calculating) {
setState(() {
_calculating = calculating;
print ("Setting state: " + calculating.toString());
});
}
The calculation is displaying but the button text never changes to "Stop" while the calculation is in progress (note the sleep for 1 second). However the print statement is printing so I know that _setCalculating is being called as expected.
Is it only possible to do one update per button press, or is there another way to go about it?