【发布时间】:2021-09-18 11:04:33
【问题描述】:
我想在单击磁贴时更改 ListTile 文本的颜色,我该怎么做呢?颜色也应该只针对特定选定的磁贴更改。 我的做法如下:
ListView.builder(
itemCount: _antigen.plantAntigens.length,
itemBuilder: (BuildContext cntxt, int index) {
return ListTile(
title: Text(
_antigen.plantAntigens[index],
style: TextStyle(
color: controller.isSelected ? Colors.red : Colors.black87),
),
onTap: () {
controller.toogle();
});
},
),
控制器代码如下:
bool isSelected = false.obs;
toogle() {
isSelected = !isSelected;
}
【问题讨论】:
-
我在使 isSelected 变量可观察时收到错误消息。即 bool isSelected = false.obs;
-
您需要使用
RxBool isSelected = false.obs。当您使用.obs时,您的返回类型现在是“可观察”类型。RxBool是 Get 的布尔值的“可观察”子类。 int、String 等也存在类似的其他类型。
标签: flutter dart flutter-getx