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?
Do I have to create more than one
TextEditingController
for everyTextField
?
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With