我在 Flutter 中尝试使用 JSON 解析方法从 RESTAPI link 获取数据。我让小部件能够选中复选框,但它选中所有而不是单一选择,我尝试了多种方法,但仍然无法正确完成。这是我的以下代码
ListView.builder(
shrinkWrap: true,
cacheExtent: 34,
primary: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return CheckboxListTile(
activeColor: Const.msinAccent,
title:
Text(snapshot.data[index].name.toString().toUpperCase()),
value: _isChecked,
secondary: CircleAvatar(
backgroundImage:
NetworkImage(snapshot.data[index].pic, scale: 13.3),
),
onChanged: (bool val) {
setState(() {
this._isChecked = val;
});
},
tristate: true,
);
},
);
我是不是做错了什么还是错过了什么?
答案 0 :(得分:0)
没关系,我找到了解决方案。
Set selectedsubs = Set();
ListView.builder(
shrinkWrap: true,
cacheExtent: 34,
primary: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return CheckboxListTile(
activeColor: Const.msinAccent,
title:
Text(snapshot.data[index].name.toString().toUpperCase()),
value: selectedsubs.contains(snapshot.data[index].name),
secondary: CircleAvatar(
backgroundImage:
NetworkImage(snapshot.data[index].pic, scale: 13.3),
),
onChanged: (bool val) {
setState(() {
this._isChecked = val;
selectedsubs.add(snapshot.data[index].name);
});
},
tristate: true,
);
},
);