flutter - 按下按钮时如何更改文本颜色?
问题描述
我在按下按钮时出现颜色变化问题,我设置了布尔条件,一切都很好,但文本的颜色没有改变?
我的代码:
TextButton(
onPressed: () {
setState(() {
pressed = !pressed;
});
},
child: Container(
margin: const EdgeInsets.all(8),
height: 60,
width: 340,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15),
),
alignment: Alignment.center,
child: Text(
'Suivant',
style: pressed
? TextStyle(color: Colors.grey)
: TextStyle(color: Colors.red),
),
),
),
解决方案
代码对我来说工作正常。我只能猜测是什么错误。
也许您的pressed
变量是在build()
方法内部声明的,而不是在State
类内部声明的。
尝试将声明移至类级别,它应该可以正常工作
推荐阅读
- git - 删除 Git 提交
- python - “设置全局变量”或“设置套件变量”关键字在机器人框架中不起作用
- python - 如何根据函数输出写入单独的列
- java - 访问由 Spring 的类转换器创建的对象上的 getter 时出现 ArrayIndexOutOfBoundsException
- python - 如何打开从 OracleDB 导出的 .xlsx 文件?
- excel - 使用 Excel VBA 解析 PDF 文档时 RPC 失败
- android - 何时需要将 RC 代码更新到 Android Q?
- excel-formula - 如何根据级别从问题库中提取测验问题
- javascript - 我在定位这个内联元素时做错了什么?
- python - 在 python 中创建嵌套列表/字典的正确方法是什么,类似于类似 json 的结构?