我是 Flutter(和 Dart)的新手,在尝试构建表单来编辑对象时,我在网上搜索了示例和教程,我看到这两种方法都被使用了。
两者有什么区别?我应该使用哪一个?
最佳答案
If you making a
Form
where you require save, reset, or validate operations- useTextFormField
. Else For Simple user input captureTextField
is sufficient.
TextFormField
,与 Form
小部件集成。
这是一个将 TextField 小部件包装在 FormField 中的便利小部件。
Form
祖先不是必需的。表单只是让一次保存、重置或验证多个字段变得更容易。
要在没有表单的情况下使用,请将 GlobalKey 传递给构造函数并使用 GlobalKey.currentState 保存或重置表单字段。
样本:
TextFormField(
autovalidateMode: AutovalidateMode.always
decoration: const InputDecoration(
icon: Icon(Icons.person),
hintText: 'What do people call you?',
labelText: 'Name *',
),
onSaved: (String value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
validator: (String value) {
return value.contains('@') ? 'Do not use the @ char.' : null;
},
)
TextField
,这是没有 Form
集成的底层文本字段。
每当用户更改字段中的文本时,文本字段都会调用 onChanged
回调。如果用户指示他们已完成在字段中的输入(例如,通过按下软键盘上的按钮),则文本字段将调用 onSubmitted
回调。
https://stackoverflow.com/questions/54661567/