Flutter:ListView.builder中的文本字段 [英] Flutter : Textfield in ListView.builder
问题描述
大家好,我在Cloud Firestore有一些数据,还有一个ListView.builder可以在其中加载数据.在此ListView.builder中,我具有Network.Image,用于显示标题和字幕的ListTile,以及另一个具有文本字段以添加一些注释和iconbutton的ListTile.我的目标是获取文本字段的文本,并在单击按钮时将其显示为alertDialog.到目前为止,我向文本字段添加了一个控制器,但是每当我在文本字段中键入任何内容时,它都会更改所有文本字段.我试图创建一个列表来指定唯一的控制器,以便我可以停止文本字段中的所有更改,但是我失败了.有什么办法可以做到这一点.所有这些文本字段和图标按钮都必须是唯一的,因此当我单击图标按钮时,我需要查看键入的文本字段的文本.
Hi everyone i have some data at Cloud Firestore and a ListView.builder to load the data in it. In this ListView.builder i have Network.Image, ListTile to show the title and subtitle and another ListTile which has a Textfield to add some comment and an iconbutton. My goal is to get the text of the textfield and show as a alertDialog when the button clicked. Until now i added a controller to the textfield but whenever i type anything in the textfield it changes all the textfields. I have tried creating a List to specify a unique controller so i can stop the all of the changes in textfields but i have failed. Is there any way i can do this. All this textfields and iconbuttons must be unique so when i clicked the iconbutton i need to see the text of the typed textfield.
推荐答案
尝试使用
List<TextEditingController> _controllers = new List();
在ListView.builder
内部为每个项目添加一个新控制器.
And inside your ListView.builder
add a new controller for each item.
ListView.builder(
itemBuilder: (context,index){
_controllers.add(new TextEditingController());
//Your code here
}),
要访问控制器的文本,您将需要索引值
To access the controller's text you will need the index value
_controllers[index].text
确保最后处置所有textController.
Make sure to dispose of all textControllers in the end.
这篇关于Flutter:ListView.builder中的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!