我有一个包含3个列表项的Listview,我需要选择一个项目,然后将其保留为选中状态;如果单击另一个,则应该取消选择先前选择的项目,并且还要更改Container的颜色,但是问题是它抛出了像这样的错误
错误:
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,
Listview的代码
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")),
),
),
),
);
},
),
),
],
),
),
}
以下代码将为您提供更多帮助。
final List<bool> physical_status = <bool>[false, false, false];
int currentSelectedIndex = 0;
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: 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,
itemCount: physical_status.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: GestureDetector(
onTap: () {
print("clicked");
setState(() {
physical_status[currentSelectedIndex] = false;
currentSelectedIndex = index;
physical_status[index] = true;
});
},
child: Container(
width: 100,
height: 100,
decoration: new BoxDecoration(
color: physical_status[index]
? Colors.red[100]
: Colors.white,
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.cover,
image: AssetImage("assets/images/user_avatar.png")),
),
),
),
);
},
),
),
],
),
),
);
}
physical_status[index].isSelected
更改为isSelected
也将color: isSelected ? Colors.red[100] : Colors.white,
放在BoxDecoration
内
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(() {
isSelected = true;
});
},
child: Container(
width: 100,
height: 100,
decoration: new BoxDecoration(
color:
isSelected ? Colors.red[100] : Colors.white,
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
"assets/images/user_avatar.png")),
),
),
),
);
},
),
),
],
),
),
您也可以创建这样的类。
class Item { final String physical_status; final bool isSelected; Item({this.physical_status, this.isSelected}); } List<Item> itemList = <Item>[ Item( physical_status: '1', isSelected: true), Item( physical_status: '2', isSelected: false), Item( physical_status: '3', isSelected: false), ];
isSelectable
列表元素访问physical_status
属性。但是元素是字符串,String
没有这种属性。您需要单独存储selectedItems
,或将physical_status
列表转换为对象列表。
我会采用第一种方法:
final List<String> physical_status = <String>['0', '1', '2'];
Set<String> physical_status_selected = Set();
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_selected.add(physical_status[index]);
});
},
child: Container(
width: 100,
height: 100,
decoration: new BoxDecoration(
color: physical_status_selected.contains(physical_status[index]) ? Colors.red[100] : Colors.white,
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
"assets/images/user_avatar.png")),
),
),
),
);
},
),
),
],
),
),