简体   繁体   English

Flutter:ListView.builder 中的文本字段

[英]Flutter : Textfield in ListView.builder

Hi everyone i have some data at Cloud Firestore and a ListView.builder to load the data in it.大家好,我在 Cloud Firestore 有一些数据和一个 ListView.builder 来加载数据。 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.在这个 ListView.builder 中,我有 Network.Image、ListTile 来显示标题和副标题以及另一个 ListTile,它有一个 Textfield 来添加一些评论和一个图标按钮。 My goal is to get the text of the textfield and show as a alertDialog when the button clicked.我的目标是获取文本字段的文本并在单击按钮时显示为 alertDialog 。 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.所有这些文本字段和图标按钮都必须是唯一的,因此当我单击图标按钮时,我需要查看键入的文本字段的文本。

Try using尝试使用

List<TextEditingController> _controllers = new List();
//try below in null-safe
//List<TextEditingController>? _controllers = [];

And inside your ListView.builder add a new controller for each item.并在您的ListView.builder中为每个项目添加一个新控制器。

ListView.builder(
            itemBuilder: (context,index){
              _controllers.add(new TextEditingController());
              //try below in null-safe
              //_controllers!.add(TextEditingController());
              //Your code here

            }),

To access the controller's text you will need the index value要访问控制器的文本,您需要索引值

_controllers[index].text

Make sure to dispose of all textControllers in the end.确保最终处理掉所有的 textController。

You might want to consider making a new type of custom Widget for each row .您可能要考虑为每一行制作一种新类型的自定义 Widget

You can leverage a您可以利用

  • TextEditingController (where you call the corresponding controller on click or the TextEditingController (您on click时调用相应的控制器或
  • TextField 's onChanged callback (where you store the new value for the corresponding item on change TextFieldonChanged回调(在其中存储on change时相应项目的新值

In both cases you have a somewhat nasty list of either text controllers or String values .在这两种情况下,您都有一个有点讨厌的文本控制器或字符串值列表

Remember, ListView.builder is only going to build the items that are in or near the viewport (as you scroll).请记住, ListView.builder只会构建视口内或附近的项目(当您滚动时)。

the builder is called only for those children that are actually visible构建器只为那些实际可见的孩子调用

That means that you can have the same row built multiple times (这意味着您可以多次构建同一行

Consider using a custom widget for each row (extend StatefulWidget )考虑为每一行使用自定义小部件(扩展StatefulWidget

This will alleviate the coordination involved with all the roles and it will push any resulting state further down the tree to the leaf nodes这将减轻与所有角色相关的协调,并将任何结果状态进一步向下推到叶节点

If using a TextEditingController :如果使用TextEditingController

  • you only have one controller to worry您只需担心一个控制器
  • call _textController.dispose() in the widget's dispose() method在小部件的dispose()方法中调用_textController.dispose()

If using a onChanged callback (available on TextField not TextFormField )如果使用onChanged回调(在TextField而不是TextFormField上可用)

  • create a String variable as state in the custom widget inputValue在自定义小部件inputValue中创建一个字符串变量作为状态
  • handle the null cases处理空情况
  • read from this when the button is tapped点击按钮时从中读取

It looks like the TextEditingController may be easiest, but I wanted to mention both options for your consideration.看起来 TextEditingController 可能是最简单的,但我想提及这两个选项供您考虑。

ListView.builder(
         shrinkWrap: true,
         physics: ScrollPhysics(),
         itemCount: list.length,
         itemBuilder: (BuildContext context, int index) {
         _controllers.add(new TextEditingController());
           return Container(
               child: TextField(
                  textAlign: TextAlign.start,
                  controller:   _controllers[index],
                  autofocus: false,
                  keyboardType: TextInputType.text,),))

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

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