This is a test where the parent widget turns green if the child container is green.
They are both blue at first, when clicking the child, both should turn green.
The child changes color when it's pressed.
class ColoredSquare extends StatelessWidget {
ColoredButton button = ColoredButton();
@override
Widget build(BuildContext context) {
return Scaffold(body: Column(children: [
button,
Container(width: 50, height: 50, color: button.color == Colors.blue ? Colors.blue : Colors.green,)]
) );
}
}
class ColoredButton extends StatefulWidget {
Color color = Colors.blue;
@override
_TestState createState() => _ColoredButtonState();
}
class _ColoredButtonState extends State<Test> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){setState(() {
widget.color = Colors.green;
});}
,child: Container(width: 50, height: 50, color: widget.color,));
}
}
Problem is, if I change state (color) of my child widget by clicking it, it's state will update, meaning it will change color just fine. However, the parent will stay the same color since it's not rebuilding, it doesnt know that the child has been updated.
How can I force the parent to rebuild?