[英]How to Remove Error Message in TextFormField in Flutter
如何删除验证器创建的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;
}
)
试试下面的代码。 错误文本没有大小调整,只有字段边框为红色。
TextFormField(
decoration: InputDecoration(
errorStyle: TextStyle(height: 0),
),
);
只需在errorStyle
中将height
设置为0
InputDecoration(errorStyle: TextStyle(height: 0)),
您可以返回一个空字符串,如果无效,该字符串仍会将边框标记为红色
validator: (String value) {
if (value.isEmpty) {
return '';
}
},
更新
我所做的修复是用 SizedBox 包裹 TextField 并给出固定高度然后想要扩展错误消息
SizedBox(
height: SOME_FIXED_HEIGHT_INCLUDING_ERROR_MESSAGE'S_HEIGHT,
child: TextField()
这对我有用。 我喜欢结果如何。
没有大小调整或类似的事情发生。
在TextFormField
的构造函数中...
decoration: InputDecoration(
isDense: true,
contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
errorMaxLines: 1,
errorText: 'Null',
errorStyle: TextStyle(
color: Colors.transparent,
fontSize: 0,
),
),
如果您愿意,可以在 SnackBar 上显示错误...
这对我来说很完美。 虽然您丢失了来自验证的错误信息,但看起来很干净。
TextFormField(
decoration: const InputDecoration(
errorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
errorStyle: TextStyle(height: 0),
),
validator: (_) => error ? '' : null,
),
这不是一种更简洁的方法,但现在我使用带有 bool 变量的error
选项,然后在hintText if error pass error message else ""
。 文本的颜色也应取决于您的错误变量。
在下面找到一个例子
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),
),
),
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 '';
},
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 + 创建一个布尔值,使错误文本替换为提示文本(这也可以用于标签文本)我想
只需像这样放置辅助文本
TextFormField(
decoration: const InputDecoration(
helperText: ' ',
),
validator: myValidator,
),
我知道为时已晚,但也许它对某人有所帮助,我一一设置了我喜欢它的方式,我的意思是,每个边框和消息都是这样的:
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,所以它不会破坏我的表单,并且当有人到达上面时,消息是空的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.