簡體   English   中英

Flutter 如何從 ListView 內的多個 TextFormField 中獲取值

[英]Flutter how to get value from multiple TextFormField inside ListView

我在ListView有多個TextFormField

List<TextEditingController>? notes = [];
List<TextEditingController>? notesMask = [];
List<TextEditingController>? remark = [];
List<TextEditingController>? remarkMask = [];

下面是ListView內的TextFormField

TextFormField(
    controller: notes![index],
    onChanged: (value) {
      if (notes1Mask![index] != value) {
        isButtonEnable = true;
      } else {
        isButtonEnable = false;
      }
    },
    decoration: InputDecoration(
      border: InputBorder.none,
      contentPadding:
          const EdgeInsets.all(10.0),
      fillColor: Colors.white,
      filled: true,
      hintText: 'Catatan',
      hintStyle: TextStyle(
          color: const Color(0xFFc0c0c0)
              .withOpacity(1)),
    ),
    style: const TextStyle(
        fontSize: 14,
        color: Color(0xFF5E5E5E)),
),

TextFormField(
    controller: remark![index],
    onChanged: (value) {
      if (notes1Mask![index] != value) {
        isButtonEnable = true;
      } else {
        isButtonEnable = false;
      }
    },
    decoration: InputDecoration(
      border: InputBorder.none,
      contentPadding:
          const EdgeInsets.all(10.0),
      fillColor: Colors.white,
      filled: true,
      hintText: 'Catatan',
      hintStyle: TextStyle(
          color: const Color(0xFFc0c0c0)
              .withOpacity(1)),
    ),
    style: const TextStyle(
        fontSize: 14,
        color: Color(0xFF5E5E5E)),
),

我的問題,如何從ListView中的TextEditingController獲取價值?

我試過print(notes) ,我得到了這個結果:

[TextEditingController#d4602(TextEditingValue(text: ┤Persiapan Sholat Shubuh 2 rokaats├, selection: TextSelection.collapsed(offset: 33, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))), 

結果,您得到的是TextEditingController列表,因為List<TextEditingController>? notes = []; List<TextEditingController>? notes = []; . 要從中獲取文本,您可以進行循環,

if (notes == null) return;
    for (final noteController in notes!) {
      print(noteController.text);
    }

要將notes文本作為列表獲取,您可以使用

List<String>? notesText =
     notes?.map((controller) => controller.text).toList();

更多關於TextEditingController

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM