提问者:小点点

按钮颜色不改变扑动/省道


我有从1号到9号的水平按钮列表。所以当我想选择或者更好地说tab a按钮时,该按钮的颜色会从白色变为蓝色。我有这个代码在下面,但它不工作!有人能帮忙吗?

class btnClass{
  final String buttonText;
  bool changeButtonColor;

  btnClass({this.buttonText, this.changeButtonColor = false});
}
 List<btnClass> listBtnClass = [
      btnClass(buttonText: '1'),
      btnClass(buttonText: '2'),
      btnClass(buttonText: '3'),
      btnClass(buttonText: '4'),
      btnClass(buttonText: '5'),
      btnClass(buttonText: '6'),
      btnClass(buttonText: '7'),
      btnClass(buttonText: '8'),
      btnClass(buttonText: '9'),
    ];
 GridView.count(
                shrinkWrap: true,
                physics: ClampingScrollPhysics(),
                mainAxisSpacing: 10,
                crossAxisSpacing: 10,
                childAspectRatio: 100 / 100,
                crossAxisCount: 10,
                children: listBtnClass.map((btnClass btnChange) {
                  return InkWell(
                    child: Container(
                        decoration: BoxDecoration(
                            color: btnChange.changeButtonColor
                                ? Colors.blue
                                : Colors.white,
                            borderRadius: BorderRadius.circular(5),
                            border: Border.all(color: Colors.grey)),
                        child: Center(
                          child: Text(btnChange.buttonText,
                          style: TextStyle(
                            fontFamily: "IranSans",
                            fontSize: 15,
                          ),
                          ),
                        )),
                    onTap: () {
                      btnChange.changeButtonColor = !btnChange.changeButtonColor;
                    },
                  );
                }).toList()),

共1个答案

匿名用户

为了刷新flutter UI,您必须调用setState()

setState((){
 btnChange.changeButtonColor = !btnChange.changeButtonColor;
});