简体   繁体   English

如何通过 onTap(){} 从 ListView.Builder 中删除 Items

[英]How to remove Items from ListView.Builder through onTap(){}

I'm trying to remove items from the List using an onTap button which is not working.我正在尝试使用不起作用的 onTap 按钮从列表中删除项目。 I have created a container with - onTap(){viewImg.remove(index);}, but the images is not being removed like it should.我用 - onTap(){viewImg.remove(index);} 创建了一个容器,但是图像并没有像它应该的那样被删除。 I believe it's an error in my code, if yes, please help me with this.我相信这是我的代码中的错误,如果是,请帮我解决这个问题。 Here is the code:这是代码:

 List<File> viewImg = List(); _imgFromGallery() async { File image = await ImagePicker.pickImage( source: ImageSource.gallery, imageQuality: 50); setState(() { _image = image; viewImg.add(image); }); } Container( margin: EdgeInsets.only(top: 50), child: ListView.builder( itemCount: viewImg.length, itemBuilder: (BuildContext context, int index) { return new SingleChildScrollView( physics: ScrollPhysics(), child: Column(children: [ Container( margin: EdgeInsets.only(top: 30), width: size.width * .8, height: 149, decoration: BoxDecoration( image: DecorationImage( image: FileImage(viewImg[index]), )), ), InkWell( onTap: () { setState(() { print('tapped'); viewImg.remove(index); }); }, child: Container( alignment: Alignment.center, width: size.width * .8, height: 40, color: Colors.white, child: Text( 'Remove', textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Roboto Medium', fontSize: 18, ), ), ), ) ]), ); }), ),

Thanks in advance.提前致谢。

You need to use the method removeAt for the position of the index.您需要对索引的位置使用 removeAt 方法。 The remove removes where the object where is equal to. remove 删除对象 where 等于的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM