选择单个复选框会选择所有其他复选框-颤动 [英] selecting a single checkbox selects all the other checkboxes - flutter
问题描述
我是新手.当我尝试选择ListView.builder中的单个卡片时,列表中的所有其他卡片都将被选择.
I am new to flutter. When I try to select a single card that in a ListView.builder, all the other cards that are in my list are selected.
我知道这是索引问题.但是,我正在使用索引来获取我从数据库获取的数据列表.因此,itembuilder中的索引是int而不是bool.
I know that this is an issue of indexing. But, i am using the indexing to got through a list of data that i'm getting from the DB. So, the index in the itembuilder is int and not bool.
我的代码
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
ServiceProvider serviceProvider = snapshot.data[index];
return new Card(
shape: selected
? new RoundedRectangleBorder(
side: new BorderSide(color: Colors.blue, width: 2.0),
borderRadius: BorderRadius.circular(4.0))
: new RoundedRectangleBorder(
side: new BorderSide(color: Colors.white, width: 2.0),
borderRadius: BorderRadius.circular(4.0)),
child: new Padding(
padding: const EdgeInsets.all(5.0),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(serviceProvider.name, style: TextStyle(fontSize: 20, height: 1.5)),
new Text(serviceProvider.serviceType, style: TextStyle(fontSize: 15, height: 1.5),),
new Text(serviceProvider.phoneNumber, style: TextStyle(fontSize: 15, height: 1.5),),
new Text(serviceProvider.address, style: TextStyle(fontSize: 15, height: 1.5),),
new Text(serviceProvider.location, style: TextStyle(fontSize: 15, height: 1.5),),
new Text(serviceProvider.emailAddress, style: TextStyle(fontSize: 15, height: 1.5),),
Container(
child: ButtonBar(
children: <Widget>[
new Checkbox(
value: selected,
onChanged: (value) {
setState(() {
selected = value;
print(selected);
});
selectedName.add(serviceProvider.name);
print(selectedName);
}),
new RaisedButton(
child: Text('Discard'),
onPressed: () {
setState(() {
deleteMethod(serviceProvider.name);
});
},
),
new RaisedButton(
child: Text('Verified'),
onPressed: () {
setState(() {
updateMethod(serviceProvider.name);
});
},
),
],
),
)
],
),
),
);
},
);```
How do i go about this?
推荐答案
保持 设置
所选对象(在您的情况下为 ServiceProvider
s,假设其hashCode和equals被正确覆盖,则可以在IntelliJ/Android Studio中通过 alt-insert
对其进行操作).然后,您可以查看集合中是否包含一个对象(以找出是否已选中),删除一个对象(以删除选择)或添加一个对象(以选择).
Keep a Set
of the selected objects (in your case, ServiceProvider
s, assuming their hashCode and equals are correctly overridden, which you can do in IntelliJ/Android Studio by alt-insert
ing them). You can then see if the set contains an object (to figure out whether or not it's selected), remove an object (to remove selection), or add an object (to select).
为此,请将以下内容保留在 State
类的顶部:
To do this, keep the following at the top of your State
class:
final selectedServiceProviders = Set<ServiceProvider>(); // creates an empty set (nothing selected by default)
然后,在其中使用 setState
更改 selected
布尔值的值时,只需使用 setState
修改 selectedServiceProviders
以及第一段中所述的相应方法( add()
或 remove()
).要检查是否选择了对象,可以调用 selectedServiceProviders.contains(object);
Then, where you use setState
to change the value of the selected
boolean, simply use setState
to modify the selectedServiceProviders
with the corresponding method described in the first paragraph (add()
or remove()
). To check whether an object is selected, you can call selectedServiceProviders.contains(object);
这篇关于选择单个复选框会选择所有其他复选框-颤动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!