简体   繁体   English

在flutter中写入时如何隐藏文本字段中的错误消息和错误边框

[英]How to hide error message and error border from textfield when writing on it in flutter

I'm trying to add validation on textfield, i want when i leave any textfield empty it change its border color into red and display a error message, so and when i write something in it then it should hide the border error and message, which is happening but not in efficient way, here is what i'm doing.我正在尝试在文本字段上添加验证,我希望当我将任何文本字段留空时,它将其边框颜色更改为红色并显示一条错误消息,因此,当我在其中写入一些内容时,它应该隐藏边框错误和消息,其中正在发生但不是以有效的方式,这就是我正在做的事情。

i created the custom textfield我创建了自定义文本字段

Widget textformfieldCustom(context,keyboardType,width,icon, controller,errortext,onchanged, hintText, labelText) {
  return Container(
    width: width,
    
    child:TextFormField(
    keyboardType:keyboardType,
    decoration: InputDecoration(
      contentPadding:EdgeInsets.symmetric(vertical: 20.0, horizontal: 10.0),
        errorText:errortext,
        labelText: labelText,
        labelStyle:  GoogleFonts.montserrat(color: HexColor("#6e6b7b")),
        hintStyle: GoogleFonts.montserrat(),
        hintText: hintText,
         prefixIcon: icon,
         focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(10.0)),borderSide: BorderSide(color: HexColor("#6610f2"))),
        border: OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(10.0))),
        errorBorder: OutlineInputBorder(
             borderRadius: BorderRadius.all(Radius.circular(10.0)),
            borderSide: BorderSide(color: Colors.red, width: 1))),
    onSaved: (String? value) {
      // This optional block of code can be used to run
      // code when the user saves the form.
    },
    onChanged:onchanged,
    controller: controller,
  ));
}

and calling it as like this并像这样称呼它

bool _validatetex = false;
textformfieldCustom(
                      context, 
                      TextInputType.number,
                      MediaQuery.of(context).size.width * 0.9,
                      Icon(Icons.email,color: iconColor,),
                      phoneNoController,
                      _validatetex ? 'This is the required field' : null,
                       (value) {
                        setState(() {
                          phoneNoController.text.isEmpty ? _validatetex = true : _validatetex = false;
                        });
                      },
                      'Enter your phone number',
                      'Phone number'
                    ),

i'm using a bool type variable in errortext and changing its state in onchanged , so i want to do it in efficient way, like if i have 10 textfields so i have to initialize 10 bool variables so this is not a good way to go.我在errortext使用bool类型变量并在onchanged更改其状态,因此我想以有效的方式进行操作,例如,如果我有 10 个文本字段,那么我必须初始化 10 个 bool 变量,因此这不是一个好方法. please help how to achieve this in efficient way.请帮助如何以有效的方式实现这一目标。

You can use the validator property in `TextField``您可以在“TextField”中使用validator属性

validator: (value) {
    if (value == null || value.isEmpty) {
      return 'This is the required field';
    }
    return null;
  },

Then your code,然后你的代码,

Widget textformfieldCustom(context,keyboardType,width,icon, controller,errortext,onchanged, hintText, labelText) {
  return Container(
    width: width,
    
    child:TextFormField(
    keyboardType:keyboardType,
    decoration: InputDecoration(
      contentPadding:EdgeInsets.symmetric(vertical: 20.0, horizontal: 10.0),
        labelText: labelText,
        labelStyle:  GoogleFonts.montserrat(color: HexColor("#6e6b7b")),
        hintStyle: GoogleFonts.montserrat(),
        hintText: hintText,
         prefixIcon: icon,
         focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(10.0)),borderSide: BorderSide(color: HexColor("#6610f2"))),
        border: OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(10.0))),
        errorBorder: OutlineInputBorder(
             borderRadius: BorderRadius.all(Radius.circular(10.0)),
            borderSide: BorderSide(color: Colors.red, width: 1))),
    onSaved: (String? value) {
      // This optional block of code can be used to run
      // code when the user saves the form.
    },
    onChanged: onchanged,
    controller: controller,
    // Validator
    validator: (value) {
      if (value == null || value.isEmpty) {
        return 'This is the required field';
      }
      return null;
    },
  ));
}

我从教程中得到了答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM