TextFormField Value to TextField New Line

1k Views Asked by At

I have a question. I want to send the return(returnvalue1...2...3) output from TextFormField to a TextField. I want it to append each output to newline without clearing the TextField. How can I do that? Thank you for your help.

TextFormField Codes:

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 Codes:

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

There are 3 best solutions below

0
On BEST ANSWER

Add controllers to both TextFormField and TextField. Then, do this when you want to append each output:

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

Append \n to the String.

This\nall\nare\nin\nnew\nlines

0
On

You need a TextEditingController to set the current string in the TextField:

final controller = TextEditingController();

Then, you will pass this controller to the field:

TextField(
  controller: controller,
  ...
)

Now, you can edit the string in the TextField whenever the value changes in the TextFormField:

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