代码之家  ›  专栏  ›  技术社区  ›  Ravindra S. Patil

在Listview中设置flutter文本字段,并在提交时获取每个文本字段的值

  •  0
  • Ravindra S. Patil  · 技术社区  · 3 年前

    控制器:

    List<TextEditingController> areaController = [];
    

    代码:

    Column(
          children: [
            ListView.builder(
              shrinkWrap: true,
              itemCount: 5,
              itemBuilder: (context, index) {
                areaController.add(TextEditingController());
                return TextField(
                  controller: areaController[index],
                  keyboardType: TextInputType.number,
                  onChanged: (text) {},
                  decoration: const InputDecoration(
                    labelText: "Area",
                  ),
                );
              },
            ),
            const SizedBox(
              height: 30,
            ),
            ElevatedButton(
              child: const Text('Submit'),
              onPressed: () {
                //get the value of every index textfield
              },
            ),
          ],
        ),
    

    我如何设置颤振 textfield 在…内 Listview 并获取每个的值 文本框 提交时

    1 回复  |  直到 3 年前
        1
  •  1
  •   MendelG    3 年前

    你为什么不使用 for -循环?只要打电话 .text 在每个 TextField 以获取文本值。

    ElevatedButton(
                  child: const Text('Submit'),
                  onPressed: () {
                    for (int i = 0; i < areaController.length; i++) {
                      print(areaController[i].text);
                    }
                  },
                ),
    
        2
  •  0
  •   Ravindra S. Patil    3 年前

    我也找到了其他方法 .map() 方法

     ElevatedButton(
          child: const Text('Submit'),
          onPressed: () {
            var fieldsValue = areaController.map((value) => value.text);
            print(fieldsValue);
          },
        )