Flutter中如何去除TextFormField中的错误信息

wen*_*nst 14 dart flutter flutter-layout

如何删除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; 
      }
 ) 
Run Code Online (Sandbox Code Playgroud)

小智 47

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

 TextFormField(
  decoration: InputDecoration(
    errorStyle: TextStyle(height: 0),
  ),
);
Run Code Online (Sandbox Code Playgroud)

  • @CliftonLabrum 这是一个 hack,但是当我在 Flutter `3.0.5` 上将 `height` 设置为 `0.01` 而不是 `0` 时,它解决了这个问题。 (11认同)
  • 甚至去掉颜色,`errorStyle: TextStyle(height: 0, color: Colors.transparent),` (3认同)
  • @RichieTarkowski hack 似乎有效,而 `TextStyle(height: 0)` 不再有效(至少,我在网络上测试了它) (2认同)

Gel*_*rge 12

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

\n

没有尺寸调整或类似的事情发生。

\n

TextFormField在...的构造函数中

\n
decoration: InputDecoration(\n        isDense: true,\n        contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),\n        errorMaxLines: 1,\n        errorText: '',\n        errorStyle: TextStyle(\n          color: Colors.transparent,\n          fontSize: 0,\n          ),\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0),\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n


Pra*_*ndo 9

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

 validator: (String value) {
      if (value.isEmpty) {
          return '';
     }
},
Run Code Online (Sandbox Code Playgroud)

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

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

Run Code Online (Sandbox Code Playgroud)


小智 6

这对我来说非常适合。看起来很干净,当然,尽管您丢失了来自验证的错误信息。

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


Blo*_*oss 6

只需设置height0errorStyle

InputDecoration(errorStyle: TextStyle(height: 0)),
Run Code Online (Sandbox Code Playgroud)