Search code examples
fluttertextwidgetflutter-widgetflutter-text

Flutter - Get text from all textfields in List of Widgets


I have a list of Container-Widgets which contain TextFields as children. Through a function, I can add/remove containers from this list.
I would like to get the text from all the TextFields once the user taps on the submit button.
Previously, I only had one large TextField and used a TextEditingController to get the data, but I'm not sure how I would do that with generated Widgets.
You can still see the maxLines and the Controller I used in the comments.

Here is also the code for the List and the function which adds the TextFields:

List<Widget> tasks = <Widget>[
  Container(
    padding: const EdgeInsets.only(top: 15, left: 15, right: 15),
    child: const TextField(
        //controller: _text,
        textInputAction: TextInputAction.done,
        //maxLines: 8,
        decoration: InputDecoration(
            hintText: "Task Description",
            hintStyle: TextStyle(color: textColor),
            enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(width: 2, color: textColor)),
            focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(width: 2, color: textColor))),
        style: TextStyle(color: textColor)),
  ),
];

void addToTaskList() {
  tasks.add(Container(
    padding: const EdgeInsets.only(left: 15, right: 15, top: 5),
    child: const TextField(
        //controller: _text,
        textInputAction: TextInputAction.done,
        //maxLines: 8,
        decoration: InputDecoration(
            hintText: "Task Description",
            hintStyle: TextStyle(color: textColor),
            enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(width: 2, color: textColor)),
            focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(width: 2, color: textColor))),
        style: TextStyle(color: textColor)),
  ));
}

Solution

  • Define the onSubmit property when you submit then save value into the list.