I've created a widget with an InkWell child, and I want to change the color of the widget when the InkWell child is being tapped. Currently I have a _pressed variable in the InkWell class to control the color of the widget:
class ButtonA extends StatefulWidget {
ButtonA();
@override
State createState() => new ButtonAState();
}
class ButtonAState extends State<ButtonA> {
bool _pressed = false;
Widget build(BuildContext context){
return new Expanded(
child: new Material(
color: _pressed ? Colors.greenAccent : Colors.redAccent,
child: new InkWell(
onTap: () => (
this.setState((){
_pressed = true;
})),
),
),
),
}
And in the parent, I just import ButtonA and create it like:
new Button()
Due to an increasing number of functions that I would love to link to this tapping behavior, I want to be able to change the button color in the parent class instead. I am thinking of passing the tap function into the Button class, so to have something like:
class ButtonA extends StatefulWidget {
VoidCallback _tap;
ButtonA(this._tap);
@override
State createState() => new ButtonAState();
}
and the InkWell will be:
child: new InkWell(onTap: () => _tap())
In the parent class, I will have a tap function that manipulate button colors and do other stuff.
How should I pass the state of the button to its parent so that maybe I can do something like
this.colors = Colors.white
in the parent class? Is it possible?