如何在颤动中强制用户在Textfield中至少输入2个字符? [英] How to force user to input a minimum of 2 characters in TextField in Flutter?

查看:0
本文介绍了如何在颤动中强制用户在Textfield中至少输入2个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在Ffltter中构建一个Textfield,用户必须在其中键入他的姓名,但我想强制他至少输入2个字符。

这是我的代码:

TextField(
                    autofocus: true,
                    textCapitalization: TextCapitalization.words,
                    textAlign: TextAlign.center,
                    decoration: InputDecoration(
                      labelStyle: TextStyle(color: Colors.blue[900]),
                      border: InputBorder.none,
                      hintText: 'VORNAME',
                    ),
                    style: TextStyle(
                        color: Colors.blue[900],
                        fontWeight: FontWeight.w600,
                        fontSize: MediaQuery.of(context).size.width * 0.075),
                    cursorColor: Colors.greenAccent,
                    inputFormatters: <TextInputFormatter>[
                      WhitelistingTextInputFormatter(RegExp("[a-zA-ZÄäÖöÜü]")) //Only Text as input
                    ],
                    onChanged: (value) {
                      userData.forename = value;
                    },
                    onSubmitted: (value) {
                      Navigator.of(context).push(
                          CupertinoPageRoute(builder: (context) => SurName()));
                    }),

我如何才能做到这一点? 提前感谢您!

推荐答案

对于文本字段验证,请使用文本格式字段而不是文本字段。 TextFormfield需要用表单小部件包装(如果有两个或更多的文本表单字段,您可以用表单小部件包装其父小部件)。表单小部件需要Key和AutoValify布尔值(这是可选的,但建议在用户尚未验证时向他们显示消息,并在他们验证消息后立即停止显示消息)。我们创建两个变量并将其分配给Form。然后,我们需要一个验证器,我们可以在其中根据我们的要求进行验证。(如果您需要添加更多验证,您可以添加多个If条件。)最后,在提交的字段中,我们检查用户是否已验证表单。如果已验证,我们将执行所需的操作,否则,我们将自动验证变量设置为True以执行自动验证。

  var formKey = GlobalKey<FormState>();
  bool autoValidate = false;


         Form(
              key: formKey,
              autovalidate: autoValidate,
              child: TextFormField(
                validator: (value){
                 return value.length < 2 ? 'Name must be greater than two characters' : null;
                },
                  autofocus: true,
                  textCapitalization: TextCapitalization.words,
                  textAlign: TextAlign.center,
                  decoration: InputDecoration(
                    labelStyle: TextStyle(color: Colors.blue[900]),
                    border: InputBorder.none,
                    hintText: 'VORNAME',
                  ),
                  style: TextStyle(
                      color: Colors.blue[900],
                      fontWeight: FontWeight.w600,
                      fontSize: MediaQuery.of(context).size.width * 0.075),
                  cursorColor: Colors.greenAccent,
                  inputFormatters: <TextInputFormatter>[
                    WhitelistingTextInputFormatter(RegExp("[a-zA-ZÄäÖöÜü]")) //Only Text as input
                  ],
                  onChanged: (value) {
                    userData.forename = value;
                  },
                  onFieldSubmitted: (value){
                    if(formKey.currentState.validate()){
                      Navigator.of(context).push(
                          CupertinoPageRoute(builder: (context) => SurName()));
                    }else{
                      setState(() {
                        autoValidate=true;
                      });
                    }

                  },
                  ),
            ),

这篇关于如何在颤动中强制用户在Textfield中至少输入2个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆