【发布时间】:2020-05-19 13:10:21
【问题描述】:
我有一个包含 3 个列表项的列表视图,我需要选择一个项目,然后它应该保持选中状态,如果单击另一个之前选择的项目应该是未选择的,并且还更改了容器颜色,但问题是它抛出这样的错误
错误:
The getter 'isSelected' isn't defined for the class 'String'.
Try correcting the name to the name of an existing getter, or defining a getter or field named 'isSelected'.
color: physical_status[index].isSelected ? Colors.red[100] : Colors.white,
列表视图代码
final List<String> physical_status = <String>['0', '1', '2'];
bool isSelected = false;
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
height: 110,
width: size.width,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: GestureDetector(
onTap: () {
print("clicked");
setState(() {
physical_status[index].isSelected = true;
});
},
child: Container(
width: 100,
height: 100,
color: physical_status[index].isSelected ? Colors.red[100] : Colors.white,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
"assets/images/user_avatar.png")),
),
),
),
);
},
),
),
],
),
),
}
【问题讨论】:
-
什么是物理状态?添加与此相关的所有代码。检查 isSelected 的数据类型可能是 String 设为 bool。
-
我已经更新了问题,isSelected 它已经设置为布尔值,它在代码中@VirenVVarasadiya