首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在颤振中使用FormField?

如何在颤振中使用FormField?
EN

Stack Overflow用户
提问于 2020-06-07 07:57:21
回答 1查看 1.3K关注 0票数 1

代码:

代码语言:javascript
复制
final GlobalKey<FormState> _formKey = GlobalKey();

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    floatingActionButton: FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: () => _formKey.currentState.validate(),
    ),
    body: Form(
      key: _formKey,
      child: FormField(
        builder: (state) => TextField(),
        validator: (string) {
          print(string); // always prints null
          return null;
        },
      ),
    ),
  );
}

每当我试图验证表单时,FormField验证器总是返回null。我认为我使用它不正确,谁能示范如何在上述情况下使用FormField

PS:

我不是在找TextFormField,我知道我的工作可以很容易地完成。

EN

回答 1

Stack Overflow用户

发布于 2020-06-07 08:15:26

根据医生的说法:

builder→FormFieldBuilder

函数,它返回表示此表单字段的小部件。它作为输入传递表单字段状态,其中包含该字段的当前值和验证状态。

在您的代码中,您将返回一个TextformField作为表示此表单字段的小部件。如果您想要验证,那么在validate小部件中使用TextFormField属性的唯一方法是:

代码语言:javascript
复制
final GlobalKey<FormState> _formKey = GlobalKey();

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    floatingActionButton: FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: () => _formKey.currentState.validate(),
    ),
    body: Form(
      key: _formKey,
      child: FormField(
        builder: (state) => TextFormField(validator: (string) {
          print(string);
          return null;
        },),
      ),
    ),
  );
}

https://api.flutter.dev/flutter/widgets/FormField-class.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62242463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档