TextFormField 值到 TextField 新行

TextFormField Value to TextField New Line

我有一个问题。我想将 TextFormField 的 return(returnvalue1...2...3) 输出发送到 TextField。我希望它在不清除 TextField 的情况下将每个输出附加到换行符。 我怎样才能做到这一点? 感谢您的帮助。

TextFormField 代码:

TextFormField(
              decoration: InputDecoration(
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: Colors.green,
                      width: 2.0,
                    ),
                  ),
                  prefixText: 'test: ',
                  prefixStyle: TextStyle(color: Colors.green, fontSize: 20),
                  errorStyle: TextStyle(
                      backgroundColor: Colors.black,
                      color: Colors.green,
                      fontSize: 18)
                  ),

              style: TextStyle(
                  fontSize: 18,
                  color: Colors.green,
                  backgroundColor: Colors.black),
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return AppLocalizations.of(context)!.returnvalue1;
                }
                if (value == girdi) {
                  return AppLocalizations.of(context)!.returnvalue2;
                } else {
                  return AppLocalizations.of(context)!.returnvalue3;
                }
              },
            ),
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 16.0),
              child: ElevatedButton(
                onPressed: () {
                  if (_formKey.currentState!.validate()) {
                  }
                },
                child: Text(AppLocalizations.of(context)!.addTextToNewLine),
              ),
            ),

TextField 代码:

 TextField(
      readOnly: true,
      showCursor: false,
      style: TextStyle(
          fontSize: 18,
          color: Colors.green,
          backgroundColor: Colors.black),
    ),

\n 附加到字符串。

This\nall\nare\nin\nnew\nlines

将控制器添加到 TextFormField 和 TextField。然后,当您想要附加每个输出时执行此操作:

textFieldController.text = textFieldController.text + textFormFieldController.text + "\n";

您需要 TextEditingController 来设置 TextField 中的当前字符串:

final controller = TextEditingController();

然后,您将把这个控制器传递给字段:

TextField(
  controller: controller,
  ...
)

现在,只要 TextFormField 中的值发生变化,您就可以编辑 TextField 中的字符串:

TextFormField(
  onChanged: (value) {
    if (value != '') {
      controller.text = "${controller.text}\n$value";
    }
  },
  ...
)