【问题标题】:How to Remove Error Message in TextFormField in Flutter如何在 Flutter 中删除 TextFormField 中的错误消息
【发布时间】:2019-10-18 22:39:04
【问题描述】:

如何删除验证器创建的TextFormField 中的错误消息?我只想要红色边框,因为我没有空间显示错误。

bool error = false;
 TextFormField ( 
      hintText:error?'please input productName':'',
      hintStyle:TextStyle(color: Colors.red), 
      validator:(v){  
       v.trim().length>0?error=false:error=true; return null; 
      }
 ) 

【问题讨论】:

标签: flutter dart flutter-layout


【解决方案1】:

试试下面的代码。 错误文本没有大小调整,只有红色的字段边框。

 TextFormField(
  decoration: InputDecoration(
    errorStyle: TextStyle(height: 0),
  ),
);

【讨论】:

  • 甚至去掉颜色,errorStyle: TextStyle(height: 0, color: Colors.transparent),
  • @SamHamada 有什么不同吗?
【解决方案2】:

您可以返回一个空字符串,如果无效,该字符串仍会将边框标记为红色

 validator: (String value) {
      if (value.isEmpty) {
          return '';
     }
},

更新

我解决这个问题的方法是用 SizedBox 包裹 TextField 并给出一个固定的高度,然后想要展开并显示错误消息

 SizedBox(
      height: SOME_FIXED_HEIGHT_INCLUDING_ERROR_MESSAGE'S_HEIGHT,
      child: TextField()

【讨论】:

  • 谢谢,我试过空字符串,它会让我的小部件向上看,你能告诉我其他解决方案吗,tks
【解决方案3】:

这对我有用。我喜欢结果。

没有大小调整或类似的事情发生。

TextFormField的构造函数中...

decoration: InputDecoration(
        isDense: true,
        contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
        errorMaxLines: 1,
        errorText: 'Null',
        errorStyle: TextStyle(
          color: Colors.transparent,
          fontSize: 0,
          ),
         ),

如果你愿意,你可以在 SnackBar 上显示错误...

【讨论】:

    【解决方案4】:

    这对我来说很完美。看起来很干净,虽然你当然会丢失来自验证的错误信息。

    TextFormField(
      decoration: const InputDecoration(
        errorBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.red),
        ),
        errorStyle: TextStyle(height: 0),
      ),
      validator: (_) => error ? '' : null,
      ),
    

    【讨论】:

      【解决方案5】:
      validator: (v) {
        v.trim().length > 0 ? error = false : error = true;
      
        // just add these 2 lines
        if (error) // if there is an error, return '' else skip it. 
          return '';
      },
      

      【讨论】:

      • 但是 return ' ' 仍然会改变小部件的空间
      • 好吧,如果你在那里传递null,那就是返回null
      • 如果返回null,则表示没有错误。
      【解决方案6】:

      这不是一个更干净的方法,但现在我使用带有布尔变量的error 选项,然后在提示文本中如果错误传递错误消息,否则""。文本的颜色也应该取决于您的错误变量。

      在下面找到一个例子

       final emailFieldColor =
              this._emailError == null ? //pass color: //pass error color;
      
                     TextField(
                          controller: controllerName, //to access value
                          textAlign: TextAlign.left,
                          decoration: InputDecoration(
                            labelText: this._emailError == null
                                  ? "Email"
                                  : this._emailError,
                            labelStyle: TextStyle(color: emailFieldColor),
                            border: InputBorder.none,
                            hintText: this._emailError == null
                                  ? "hint text"
                                  : this._emailError,
                            hintStyle: TextStyle(color: 
                                      this._emailError == null
                                      ? //color1
                                      : //color2),
                          ),
                        ),
      

      【讨论】:

      • ``` bool error = false; TextFormField (hintText:error?'please input productName':'',hintStyle:TextStyle(color: Colors.red), validator:(v){ v.trim().length>0?error=false:error=true; return null; }) ``` 我试过这样的代码,但只有焦点小部件才会显示提示文本,当验证为假时如何显示提示文本,或者我误解了,谢谢
      • 我认为你已经成功了,如果没有,请告诉我我会为你写一个例子。
      • 我确实像你说的那样,我在验证之前隐藏了hintText,但是在验证之后,hintText 直到焦点在小部件上才会显示,你能给我举个例子吗,谢谢
      • 我在答案中添加了我的例子:)
      【解决方案7】:
      hintText: validation ? NAME_TEXT_FIELD_HINT : 'Name is required!',
                    hintStyle: TextStyle(fontSize: 17.0),
                    errorStyle: TextStyle(height: 0),
                    contentPadding:
                        EdgeInsets.symmetric(horizontal: 32.0, vertical: 14.0)),
                validator: (String value) {
                  if (value.trim().isEmpty) {
                    setState(() {
                      validation = false;
                    });
                    return '';
                  }
                  return null;
      

      这对我有用,将高度设置为 0 + 创建一个布尔值,使错误文本替换提示文本(这也可以用于标签文本)我想

      【讨论】:

        【解决方案8】:

        只需要这样的帮助文本

        TextFormField(
          decoration: const InputDecoration(
            helperText: ' ',
          ),
          validator: myValidator,
        ),
        

        【讨论】:

          【解决方案9】:

          我知道为时已晚,但也许它对某人有帮助,我一一设置我喜欢它的方式,我的意思是,每个边框和消息都是这样的:

           TextFormField(
                                    controller: controller.emailController,
                                    decoration: InputDecoration(
                                      errorBorder: OutlineInputBorder(
                                        borderSide: BorderSide(color: Colors.grey),
                                        borderRadius: BorderRadius.only(
                                          topRight: Radius.elliptical(120, 120),
                                        ),
                                      ),
                                      disabledBorder: OutlineInputBorder(
                                        borderSide: BorderSide(color: Colors.grey),
                                        borderRadius: BorderRadius.only(
                                          topRight: Radius.elliptical(120, 120),
                                        ),
                                      ),
                                      focusedErrorBorder: OutlineInputBorder(
                                        borderSide: BorderSide(color: Colors.grey),
                                        borderRadius: BorderRadius.only(
                                          topRight: Radius.elliptical(120, 120),
                                        ),
                                      ),
                                      errorStyle:
                                          TextStyle(height: 0, color: Colors.amber),
                                      prefixIcon: Icon(Icons.person),
                                      hintText: 'Username',
                                      border: OutlineInputBorder(
                                        borderRadius: BorderRadius.only(
                                          topRight: Radius.elliptical(120, 120),
                                        ),
                                      ),
                                      //fillColor: Colors.green
                                    ),
                                    autofocus: true,
                                    keyboardType: TextInputType.emailAddress,
                                    validator: (value) => '',
                                    onSaved: controller.onEmailSaved,
                                    onFieldSubmitted: controller.requestPasswordFocus,
                                  ),
          

          它们现在都是一样的,错误消息的高度也是 0,所以它不会破坏我的表单,并且当有人到达上面时消息是空的。

          【讨论】:

            猜你喜欢
            • 2021-09-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-05-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-25
            相关资源
            最近更新 更多