我试图改变SVG的颜色,当我点击它。我在parent widget
中使用boolean
,并在svg被选中时将其设置为true/false。然后我在我的child widget
中阅读boolean
,并试图通过conditional logic
为孩子设置颜色。在debugging
中,我可以看到boolean
更改为true
和false
。但是颜色不会改变。在这种情况下如何改变颜色?
错误:No errors, it just don't change colour when tap
儿童小部件
class ToolSetButton extends StatefulWidget {
final double width;
final double height;
...
const ToolSetButton(
{Key? key,
...
this.checkedColour = iconColourUnchecked,
this.unCheckedColour = iconColourChecked,
...
required this.buttonType ,
})
: super(key: key);
@override
State<ToolSetButton> createState() => _ToolSetButtonState();
}
class _ToolSetButtonState extends State<ToolSetButton> {
@override
Widget build(BuildContext context) {
return Column(children: <Widget>[
InkWell(
splashColor: Colors.white,
onTap: () {
widget.functionReceiverforButtonandSVG();
},
child: Container(
child: Padding(
padding: EdgeInsets.all(
functionCalculatePadding(widget.height)),
child: FittedBox(
fit: BoxFit.contain,
child: widget.icon == null
? SvgPicture.asset(
widget.svgPicPath!,
color:
buttonChecked == true
? widget.checkedColour
: widget.unCheckedColour
)
父部件
class _ToolSetBottomState extends State<ToolSetBottom> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: [
ToolSetButton(
svgPicPath: svgVectorImagePath,
buttonType: myButtonTypeList.SVGOnly,
checkedColour: Colors.green,
unCheckedColour: Colors.grey,
width: 80,
height: 80,
functionReceiverforButtonandSVG: () { funcForTapsandClicks (myButtonList.Two);}),
funcForTapsandClicks( myButtonList tappedOn) {
if (tappedOn == myButtonList.Two ){
print('Two');
buttonChecked = buttonChecked == true
?false : true;
}
}
将变量buttonchecked发送给子小部件,而不是传递函数。在子小部件中使用setState((){ widget.buttonChecked = !widget.buttonChecked; });