Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using TextEditingController more than once

Tags:

flutter

dart

Do I have to create more than one TextEditingController for every TextField example

var oneController = TextEditingController();
var twoController = TextEditingController();

then

TextField(
   controller: oneController,
   decoration: new InputDecoration(labelText: "add type *income"),
),
TextField(
   controller: twoController,
   decoration: new InputDecoration(labelText: "Enter a number"),
   keyboardType: TextInputType.number,
),

or is there a way to use just one?

like image 997
Kagimura Avatar asked Oct 13 '19 00:10

Kagimura


2 Answers

Do I have to create more than one TextEditingController for every TextField?

Yes. A controller allows you to control and access the current state of the field,

On this case, it allows you to know what is the current user input, so you can read or change that value, and allows you to select or see what's selected by the user. It can also provide an initial value for each field.

If you use the same controller, the same thing will happen in both TextFields at the same time, for instance, when the user types a letter on one field, it shows up on the other as well.

If you have a form or too many fields, you may want to use other solutions like the TextFormField, which can also be used without a controller to validate and save user input all at once for multiple fields inside a Form Widget.

More about the TextEditingController on the docs.

like image 147
George Avatar answered Oct 07 '22 20:10

George


You can use Something Similar to this. Then Simply use them like array wherever you want to.

List<TextEditingController> myController =
      List.generate(5, (i) => TextEditingController());
like image 32
Ibrahim Khwaja Avatar answered Oct 07 '22 21:10

Ibrahim Khwaja