在颤动中单击按钮时如何更改 SVG 图片的颜色?

时间:2021-01-08 15:27:12

标签: flutter

我目前正在制作学校课程应用程序,该应用程序显示学生必须学习的一些课程。

这是我现在正在处理的简短代码。

class courseBoxes extends StatefulWidget {
  String labelText;
  bool selected;
  double xSize;

  courseBoxes({Key key, @required this.labelText, this.xSize,this.selected})
      : super(key: key);

  @override
  courseBoxState createState() => courseBoxState();
}

class courseBoxState extends State<courseBoxes> {

  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: ChoiceChip(
          padding: EdgeInsets.only(bottom: 8),

          label: Container(
            child: Text(
              widget.labelText,
              style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.w400,
                fontSize: 14,
              ),
            ),
          ),
          visualDensity: VisualDensity.standard,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(4.0),
            ),
            side: BorderSide(
              color: widget.selected == true ? Color(0xff6d69fb) : Color(0xfff3f2ff)
            )
          ),
          selected: widget.selected,
          labelStyle: TextStyle(color: Colors.white),
          backgroundColor: Color(0xfff3f2ff),
          selectedColor: Color(0xffbeb9ff),
          onSelected: (selected) {
            setState(
              () {
                widget.selected = !widget.selected;
              },
            );
          },
        ),
      ),
      height: 26,
      width: widget.xSize,
    );
  }
}

此代码只不过是通过检查 bool selected 值来制作一个单击时会改变颜色的课程框,就像下面的 gif 一样。

enter image description here

问题是这样的。当我将 bool 变量传递给这个 class courseBoxes 时,我传递的 bool 变量似乎根本没有更新。 例如,

class certificateguidelineState extends State<certificateguideline> {
  bool _arrowSelect3=true;
  ...
      Container(
          padding: EdgeInsets.fromLTRB(136, 144, 0, 0),
          child: courseBoxes(labelText: "공업수학",xSize: 103,selected: this._arrowSelect3,),
      ),

这很好用,但是这个不行。

....
Container(
            padding: EdgeInsets.fromLTRB(270, 92, 0, 0),
            child: SvgPicture.asset(
            'asset/arrowSVG/arrow2.svg',color: _arrowSelect3 == true ? Colors.black : Colors.blue
           ),
),


我认为传递 bool _arrowSelect3 也会影响这个容器,因为 class courseBoxes 通过按下它的按钮来改变 _arrowSelect3 的值..但我发现我完全错误的方式.. . 有什么解决办法吗?

0 个答案:

没有答案