在我的代码中,我验证了电话号码。如果电话号码不正确 - 我会显示错误消息。但是,当用户开始编辑号码时,我想隐藏此错误消息。
我已经找到了解决方案currentState.reset()
,但似乎不是很好的解决方案。我必须处理保存文本和光标位置的问题。而且我还有一件小神器。通常当我按住退格键时 - 它会一一删除符号。如果我在显示错误消息时执行此操作 - 则错误消息会消失并且只删除一个符号。
有人知道这种情况的正确解决方案吗?
final TextEditingController controller = TextEditingController();
final RegExp _phoneRegex = RegExp(r"^\+{1}\d{10, 15}\$");
bool isError = false;
TextSelection currentPosition;
return Column(
children: <Widget>[
Form(
key: _textKey,
child: TextFormField(
controller: controller,
validator: (str) {
isError = true;
if (str.isEmpty) {
return err_empty_field;
} else if (!_phoneRegex.hasMatch(str)) {
return err_invalid_phone;
}
isError = false;
},
),
onChanged: () {
if (controller.selection.start < 0 &&
controller.text.length > 0) {
TextSelection position =
controller.text.length > …
Run Code Online (Sandbox Code Playgroud) 如何删除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)