flutter stateful widgte non final field code example

Example: flutter stateful widgte non final field

class TestButton extends StatefulWidget {
  TestButton({this.passedcolor});
  final Color passedColor;
  @override
  _TestButtonState createState() => _TestButtonState();
}

class _TestButtonState extends State<TestButton> {
  Color color;

  @override
  initState(){
   color = widget.passedColor;
   super.initState()
  }

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        setState(() {
          color = color == Colors.red ? Colors.blue : Colors.red;
        });
      },
      child: Icon(
        Icons.add,
        size: 80,
      ),
      color: color,
    );
  }
}

Tags:

Misc Example