简体   繁体   English

按钮的颜色不会改变颤振/飞镖

[英]Color of button does not change flutter/Dart

I have list of horizontal buttons from number 1 to number 9. so when I want to select or better to say tab a button, that button changes color from white to blue.我有从数字 1 到数字 9 的水平按钮列表。所以当我想要 select 或更好地说是一个按钮时,该按钮的颜色会从白色变为蓝色。 I have this code below down here but it's not working?我在下面有这段代码,但它不起作用? can anyone help?谁能帮忙?

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()),

in order for flutter UI to refresh you have to call setState()为了让 flutter UI 刷新你必须调用 setState()

setState((){
 btnChange.changeButtonColor = !btnChange.changeButtonColor;
});
    class Deneme extends StatefulWidget {
  @override
  _DenemeState createState() => _DenemeState();
}

class _DenemeState extends State<Deneme> {
  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'),
  ];
  @override
  Widget build(BuildContext context) {
    return 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: () {
              setState(() {
                btnChange.changeButtonColor = !btnChange.changeButtonColor;
              });
            },
          );
        }).toList());
  }
}

class btnClass {
  final String buttonText;
  bool changeButtonColor;

  btnClass({this.buttonText, this.changeButtonColor = false});
}

This is the full version of my first answer.这是我第一个答案的完整版本。 You can compare it with your code.您可以将其与您的代码进行比较。 I couldn't understand where the problem is if you tried setState.如果您尝试 setState,我无法理解问题出在哪里。 But I'm sure this is working, I tried.但我确定这是有效的,我试过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM