我有从sqflite数据库加载到GridView.builder中的项目。因为它的模型类不是有状态的,当然,我不能在那里的项目上创建Select效果。
我所说的select效果是这样的:

当用户点击某个项目时,该项目处于选中状态
GridView.builder(
gridDelegate:.....,
itemBuilder: (BuildContext context, int index) {
bool _selectItem = false;
return Stack(
children: <Widget>[
itemsList[index]), //====Actual Item=====//
InkWell(onTap: () { //===To create Select Effect====//
setState(() {
if (_selectItem == false) {
_selectItem = true;
print("Item Selected");
} else {
_selectItem = false;
print("Item UnSelected");
}
});
},
child: Opacity(
opacity: _selectItem == true ? 0.5 : 0.0,
child: Icon(Icons.select)
),]); },
itemCount: itemsList.length,
))我可以创建一个选择效果,但如果我点击任何一个项目,它就会选择所有项目。如何为每个单独的项目创建选择效果。
那么我如何为每个单独的项目创建选择效果呢?
附言:我只在代码中写了相关的东西
发布于 2019-04-08 13:09:20
由于没有对itemList index进行选择和取消选择的管理,您的项目数据从sqflite数据库加载到GridView.builder中,因此您将通过GridView中的局部变量手动管理选择,
当第一次将项目插入到表中时,需要将选择字段插入到项目表中,而不是局部变量。
InkWell(onTap: () { //===To create Select Effect====//
setState(() {
itemList[index].selectItem = !itemList[index].selectItem
});
},然后在onTap之后管理项目选择
https://stackoverflow.com/questions/55540983
复制相似问题