首页 > 解决方案 > 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(????));
});

} }

我试图弄清楚如何删除它。

当这个列表是一个文本列表时,我可以根据下一个添加和删除。但是现在一个容器被添加到列表中,我想删除该索引处的项目,或者整个容器。

标签: flutterdart

解决方案


您是否尝试过这样的方法,比较标签是否与您的原始列表匹配,

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()));

推荐阅读