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

zy1mlcev  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(221)

控制器:

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
          },
        ),
      ],
    ),

如何在Listview中设置flutter textfield并在提交时获取每个textfield的值

u0sqgete

u0sqgete1#

你为什么不用for环?只需在每个TextField上调用.text来获取文本值。

ElevatedButton(
              child: const Text('Submit'),
              onPressed: () {
                for (int i = 0; i < areaController.length; i++) {
                  print(areaController[i].text);
                }
              },
            ),

相关问题