Flutter 如何删除颤振中TextFormField中的错误消息
如何删除验证程序创建的Flutter 如何删除颤振中TextFormField中的错误消息,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,如何删除验证程序创建的TextFormField中的错误消息?我只想要红色边框,因为我没有显示错误的空间 bool error = false; TextFormField ( hintText:error?'please input productName':'', hintStyle:TextStyle(color: Colors.red), validator:(v){ v.trim().length>0?error=fal
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;
}
)
您可以返回一个空字符串,如果无效,该字符串仍会将边框标记为红色
validator: (String value) {
if (value.isEmpty) {
return '';
}
},
更新
我所做的是用SizedBox包装文本字段,并给出一个固定的高度,然后用错误消息展开
SizedBox(
height: SOME_FIXED_HEIGHT_INCLUDING_ERROR_MESSAGE'S_HEIGHT,
child: TextField()
这不是一种更简洁的方法,但目前我正在使用带有bool变量的
error
选项,然后在hintText中使用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),
),
),
这是为我工作。我喜欢结果 没有大小调整或类似的事情发生 在
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: InputDecoration(
errorStyle: TextStyle(height: 0),
),
);
这对我有效,将高度设置为0+创建一个布尔值,使错误文本取代hintText(这也可以用于标签文本)。我想这对我来说非常有效。看起来很干净,当然,您丢失了来自验证的错误信息
TextFormField(
decoration: const InputDecoration(
errorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
errorStyle: TextStyle(height: 0),
),
validator: (_) => error ? '' : null,
),
我知道已经很晚了,但也许这对某人有帮助,我一个接一个地设定了我喜欢的方式,我的意思是,每个边框和信息如下:
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,这样就不会破坏我的表单,并且消息是空的,正如上面有人提到的。只是
返回空@Blasanka返回空将删除validatorCheck的功能回答:谢谢,我已经尝试了空字符串,它会使我的小部件加速,你能告诉我另一种解决方案吗,tks``bool error=false;TextFormField(hintText:error?’请输入产品“:”,hintStyle:TextStyle(颜色:Colors.red),验证器:(v){v.trim().length>0?error=false:error=true;return null;})``我试过像这样的代码,但只有focus小部件才会显示hintText,当validate为false时,我如何显示hintText,或者我误解了,谢谢你我想你已经成功了,如果没有,请告诉我我会为你写一个例子。我确实像你说的,我在验证之前隐藏了hintText,但是在验证之后,hintText在关注小部件之前不会显示,你能给我一个例子吗,谢谢,我在答案中添加了我的示例:)但是返回“”仍然会更改WidgetOK的空间,如果在那里传递null,那会怎么样呢?如果返回null,那就意味着没有错误。即使去掉颜色,errorStyle:TextStyle(高度:0,颜色:Colors.transparent),
@SamHamada这有什么区别吗?
TextFormField(
decoration: const InputDecoration(
errorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
errorStyle: TextStyle(height: 0),
),
validator: (_) => error ? '' : null,
),
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,
),