I create a custom button that changes it's Image and Text color based on the bool pressAttention
parameter.
class UserButton extends StatefulWidget {
final String unselectedImagePath;
final String selectedImagePath;
final String text;
UserButton({
this.unselectedImagePath,
this.selectedImagePath,
this.text,
});
@override
State<StatefulWidget> createState() => _UserButtonState();
}
class _UserButtonState extends State<UserButton> {
bool pressAttention = false;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Ink.image(
image: pressAttention
? AssetImage(widget.selectedImagePath)
: AssetImage(widget.unselectedImagePath),
fit: BoxFit.cover,
width: 150.0,
height: 150.0,
child: InkWell(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
pressAttention = !pressAttention;
});
},
),
),
Padding(
padding: EdgeInsets.only(top: 30.0),
child: Text(
widget.text,
style: TextStyle(
color: pressAttention
? Theme.of(context).accentColor
: Colors.white,
fontFamily: "Roboto",
fontSize: 18.0
),
),
)
],
);
}
}
And then inflated them in my main class like this:
Padding(
padding: EdgeInsets.only(top: 100.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
UserButton(
selectedImagePath: 'assets/whistle_orange.png',
unselectedImagePath: 'assets/whistle.png',
text: "Coach",
),
Container(width: 30.0,),
UserButton(
selectedImagePath: 'assets/weight_orange.png',
unselectedImagePath: 'assets/weight.png',
text: "Student",
)
],
),
),
While the two buttons are working correctly by themselves I need to disable the first one (change the pressAttention
and call setState()
) and viceversa.
How can I achieve that?