flutter - Flutter 从另一个小部件中删除列表项
问题描述
我有一个生成列表的列表视图生成器
ListView.builder(
itemCount: snapshot.length,
itemBuilder: (context, index) {
return Tag(
callback: (list) => setState(() => hashList = list),
tag: snapshot[index].data["title"],
list: hashList,
);
},
)
tag 函数然后根据它是否被选中将项目添加到另一个列表中,如果再次单击则将其删除。
这是 Tag 中的句柄选择函数
void _handleOnPressed() {
setState(() {
isSelected = !isSelected;
isSelected
? _animationController.forward()
: _animationController.reverse();
isSelected ? widget.list.add(
Container(
height: 30,
color: Colors.green,
child: Text(widget.tag, style: TextStyle(color: Colors.purple),),
)): widget.list.removeAt(????));
});
} }
我试图弄清楚如何删除它。
当这个列表是一个文本列表时,我可以根据下一个添加和删除。但是现在一个容器被添加到列表中,我想删除该索引处的项目,或者整个容器。
解决方案
您是否尝试过这样的方法,比较标签是否与您的原始列表匹配,
int removeTagAt(){
for(int i=0;i<snapshot.lenght;i++)
{
if(widget.tag==snapshot[index])
return i;
}
}
然后打电话,
Container(
height: 30,
color: Colors.green,
child: Text(widget.tag, style: TextStyle(color: Colors.purple),),
)): widget.list.removeAt(removeTagAt()));
推荐阅读
- javascript - AWS Amplify/Cognito:如何正确检查用户是否经过身份验证?
- python - 如果组合框值为 selectek Tkinter Python3,则显示标签
- ruby-on-rails - 在哪里保持大的常量哈希
- python - 如何删除 tkinter 菜单分隔符
- java - 在 Spring Security 中添加新用户时出现异常
- jquery - 检查复选框上的所有复选框单击jquery数据表
- sql - 如果表中存在值,如何返回布尔值
- c - 从文件读取并将信息放入 C 中的结构的问题(结构中的不同 var、返回错误、具有 var modif 类型的非嵌套函数)
- python - 在给定 2 张图像的情况下查找对象的 3D 位置
- javascript - 当我在 Javascript 中调用它时,样式不适用于我的代码的一部分?