繁体   English   中英

选中时如何更改凸起按钮的颜色

[英]How to change the color of raisedButton when selected

我想做一些非常简单的事情,但我认为我错过了一个步骤,因为它不起作用。 我需要做的是在选中时更改 raiseButton 的样式。

到目前为止,我是这样做的:

Container ChoiceChip(String chipName) {
    bool isButtonPressed = false;
    return Container(
      child: RaisedButton(
        color: isButtonPressed ? Colors.green : Colors.red,
        child: Text(
          chipName,
          style: TextStyle(
            color: Colors.black,
          ),
        ),
        onPressed: () {
          setState(() {
            isButtonPressed = !isButtonPressed;
          });
        },
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0),
          side: BorderSide(
            color: isButtonPressed ? Colors.green : Colors.red,
          ),
        ),
      ),
    );
  }

如果我在 onPressed 上打印一些东西,它会显示在终端中,但它不会改变按钮的颜色

在 BuildContext 之前在无状态/有状态小部件中定义布尔值。 然后使用 initState 在启动时存储布尔值的状态。 最后确保您完全重新启动应用程序以重新初始化应用程序。

暂无
暂无

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

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