Say I want to create a button that every time that it's clicked it changes color. Its starting color is required in the constructor.
The following code works fine, but when I hover on top of TestButton I get this message: "This class (or a class which this class inherits from) is marked as '@immutable', but one or more of its instance fields are not final: TestButton.color".
If it should be final but I need it to change, what's the solution? Why does it have be final if it works anyways?
class TestButton extends StatefulWidget {
TestButton({this.color});
Color color;
@override
_TestButtonState createState() => _TestButtonState();
}
class _TestButtonState extends State<TestButton> {
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () {
setState(() {
widget.color = widget.color == Colors.red ? Colors.blue : Colors.red;
});
},
child: Icon(
Icons.add,
size: 80,
),
color: widget.color,
);
}
}