我创建了一个自定义按钮,根据bool pressAttention
参数更改它的图像和文本颜色。
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
),
),
)
],
);
}
}
然后像这样在我的主课中膨胀它们:
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",
)
],
),
),
虽然这两个按钮本身可以正常工作,但我需要禁用第一个(更改pressAttention
and call setState()
),反之亦然。
我怎样才能做到这一点?