gpt4 book ai didi

flutter - 如何在Flutter中从API选择索引并将其分别删除?

转载 作者:行者123 更新时间:2023-12-03 03:50:59 27 4
gpt4 key购买 nike

我正在从API获取图像并将其显示到网格 View 中,但要求是我长按图像的任何索引,所选图标应在该索引图像上可见。
但是问题是当我长按任何索引时,所选图标在所有索引上都可见。
屏幕截图:
enter image description here
为了解决这个问题,我制作了模型类,其中有数据类型
第一个是每个索引的 bool(boolean) 变量(isSelected),第二个是从API获取的PhotoDetails,但是无法使用FutureBuilder处理它,因为当我执行setState并且isSelected变为false时,它将重建build方法。
码:
型号类别:

class Photos{
PhotoDetail photoDetail;
bool isSelected;
Photos({this.photoDetail, this.isSelected});
}
FutureBuilder:
 Expanded(
child: FutureBuilder<PhotoModel>(
future: _photoApi.getPhotosByUserList(
token: widget.tokenId,
contactId: widget.userContent.id,

),

builder:(BuildContext context, AsyncSnapshot<PhotoModel> snapshot){
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}

if (snapshot.hasError){
return Center(child: new Text('Error: ${snapshot.error}'));
}
List<Photos> photos =[];
snapshot.data.content.forEach((element) {
photos.add(
Photos(
isSelected: false,
photoDetail: element
)
);
});
print("photos photos photos length:${photos.length}");
return photos.length>0?
sliverGridWidget(context,photos)
:Container(
alignment: Alignment.center,
child: Text("Empty"),
);
}

)

)

网格 View 中的图像:
 Widget sliverGridWidget(BuildContext context, List<Photos> listPhotoDetail){
return StaggeredGridView.countBuilder(
padding: const EdgeInsets.all(8.0),
crossAxisCount: 6,
itemCount: listPhotoDetail.length,
itemBuilder: (context, index){
return InkWell(
onLongPress: (){
setState(() {
enable = true;
print("iinnndexxxxxxx:$index");
// listPhotoDetail[index].isSelected = true;
});
},

child: Container(
alignment: Alignment.bottomRight,
decoration: BoxDecoration(
color:Colors.grey[100],

image: DecorationImage(
image: NetworkImage(listPhotoDetail[index].photoDetail.image.fileUrl),
fit: BoxFit.cover
)
),
child:enable?
Image.asset('assets/icons/selected.png')
:Container()
),
);
},
staggeredTileBuilder: (index)=> view ?StaggeredTile.count(6,6):StaggeredTile.count(2,2),
mainAxisSpacing: 8.0,
crossAxisSpacing:8.0,
);
}

最佳答案

为了解决这个问题,请尝试对每个图像使用特定的键

关于flutter - 如何在Flutter中从API选择索引并将其分别删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63493185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com