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)
Gel*_*rge 12
这对我有用。我喜欢它的结果。
\n没有尺寸调整或类似的事情发生。
\nTextFormField在...的构造函数中
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),\nRun Code Online (Sandbox Code Playgroud)\n如果您愿意,可以在 SnackBar 上显示错误...
\n您可以返回一个空字符串,如果无效,该字符串仍会将边框标记为红色
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)
只需设置height为0errorStyle
InputDecoration(errorStyle: TextStyle(height: 0)),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15610 次 |
| 最近记录: |