我是否必须创建多个 TextEditingController
每 TextField
例子
var oneController = TextEditingController();
var twoController = TextEditingController();
然后
TextField(
controller: oneController,
decoration: new InputDecoration(labelText: "add type *income"),
),
TextField(
controller: twoController,
decoration: new InputDecoration(labelText: "Enter a number"),
keyboardType: TextInputType.number,
),
或者有没有办法只使用一个?
最佳答案
Do I have to create more than one
TextEditingController
for everyTextField
?
是的。 Controller 允许您控制和访问字段的当前状态,
在这种情况下,它允许您知道当前用户输入是什么,因此您可以读取或更改该值,并允许您选择或查看用户选择的内容。它还可以为每个字段提供初始值。
如果你使用同一个 Controller ,同样的事情会同时发生在两个 TextFields 中,例如,当用户在一个字段上输入一个字母时,它也会显示在另一个字段上。
如果您有一个表单或太多字段,您可能需要使用其他解决方案,例如 TextFormField ,它也可以在没有 Controller 的情况下用于为 Form 中的多个字段一次验证和保存用户输入。小部件。
更多关于
TextEditingController
on the docs .
关于flutter - 多次使用 TextEditingController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58359607/