android-studio - Flutter TextFormField Validation 未显示,并给出非活动 InputConnections 的错误
问题描述
所以,我一直在寻找一种方法来解决 Flutter TextFormField 不显示验证器的错误消息的问题。这是我的注册屏幕代码
import 'package:flutter/material.dart';
import 'package:messenger/widgets/widget.dart';
class SignUp extends StatefulWidget {
@override
_SignUpState createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
final formKey= GlobalKey<FormState>();
TextEditingController usernameTextEditingController= new TextEditingController();
TextEditingController emailTextEditingController= new TextEditingController();
TextEditingController passwordTextEditingController= new TextEditingController();
String value;
bool isLoading=false;
signMeUp(){
if(formKey.currentState.validate()){
setState(() {
isLoading=true;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black87,
appBar: appBarMain(context),
body: isLoading?Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [0.1,0.5,0.8,1],
colors: [Colors.yellow,Colors.orangeAccent,Colors.deepOrange, Colors.redAccent]),
borderRadius: BorderRadius.circular(20)),
margin: EdgeInsets.symmetric(horizontal: 24),
padding: EdgeInsets.symmetric(horizontal: 20),
alignment: Alignment.center,
child: CircularProgressIndicator(),):Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [0.1,0.3,0.7,1],
colors: [Colors.yellow,Colors.orangeAccent,Colors.deepOrange, Colors.redAccent]),
borderRadius: BorderRadius.circular(20)),
margin: EdgeInsets.symmetric(horizontal: 24),
padding: EdgeInsets.symmetric(horizontal: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Form(
key: formKey,
child: Column(
children: <Widget>[
TextFormField(
validator: (val){
return val.isNotEmpty || val.length < 4 ? val="Username is not valid":val =null;
},
autofocus: false,
controller: usernameTextEditingController,
style: textFieldTextStyle(),
decoration: textFieldInputDecoration("Username"),
),
TextFormField(
validator: (val){
return RegExp(r'^.+@[a-zA-Z]+\.{1}[a-zA-Z]+(\.{0,1}[a-zA-Z]+)$').hasMatch(val)? null:"Email Id is not valid";
},
autofocus: false,
controller: emailTextEditingController,
style: textFieldTextStyle(),
decoration: textFieldInputDecoration("Email"),
),
TextFormField(
obscureText: true,
validator: (val){
return val.length>6?null:"Password should be greater than 6 characters";
},
controller: passwordTextEditingController,
style: textFieldTextStyle(),
decoration: textFieldInputDecoration("Password"),
),
],
),
),
SizedBox(height: 15,),
Container(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16,vertical: 8),
alignment: Alignment.bottomCenter,
child: Text("By clicking Sign Up, you agree to our Policies",style: TextStyle(color: Colors.white,fontSize: 13,fontWeight: FontWeight.w300))),
),
SizedBox(height: 15,),
GestureDetector(
onTap: (){
signMeUp();
},
child: Container(
child: buttonToUse("Sign Up",Colors.orangeAccent)
),
),
SizedBox(height: 4,),
Container(
child: buttonToUse("Sign Up with Google",Colors.lightBlueAccent)
),
SizedBox(height: 10,),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Already have an Account? ",style: textFieldTextStyle(),),
GestureDetector(
onTap: (){
Navigator.pop(context);
},
child: Text("Sign In",style: TextStyle(color: Colors.white,fontSize: 16,fontWeight: FontWeight.w400,
decoration: TextDecoration.underline)),
),
],
),
SizedBox(height: 30,)
],
),
),
);
}
}
以及我在这些表单字段中输入内容时遇到的错误
W/IInputConnectionWrapper(10562): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(10562): endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(10562): setComposingRegion on inactive InputConnection
W/IInputConnectionWrapper(10562): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(10562): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(10562): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(10562): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(10562): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(10562): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(10562): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(10562): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(10562): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(10562): requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper(10562): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(10562): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(10562): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(10562): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(10562): endBatchEdit on inactive InputConnection
另外,我在我的应用程序上没有看到任何验证器错误这是一个屏幕截图 即使按下注册按钮后也没有红线显示错误
顺便说一句,如果我能得到你对更好用户界面的看法,那就太好了
解决方案
如果您希望在用户键入时显示验证错误,则必须autovalidate
将表单的属性设置为。true
关于 UI 的状态,我不建议在isLoading
is时用加载指示器替换整个视图true
。我建议您更改注册按钮。如果您希望在加载时无法访问整个屏幕,则必须使用各种平视显示对话框。这将使整个屏幕可见,但用户将无法与表单元素进行交互。您可以查看此库modal_progress_hud
此外,考虑到您正在使用的渐变,使用默认的红色来显示错误文本可能不是最好的,尝试使用其他一些颜色来提高错误消息的可见性
推荐阅读
- angular - Debugging Angular TFS extension
- openssl - OpenSSL/KeyTool - 如何从密钥库获取客户端 crt
- android - 如何在 Flutter 中配置应用内购买
- bash - 按空格分割字符串,循环结果按破折号分割
- spring - 在 Kotlin 中找不到使用 QuerydslBinderCustomizer 的类型的属性自定义
- go - 如何从同一函数返回未知数据类型?(哥朗)
- scala - 有没有办法从 JAR 中提取 SCALA 源代码?
- vue.js - 使用vuejs输入为空时如何禁用按钮?
- c++ - 为什么 push_back() 在向量的开头创建两个零
- node.js - axios调用node js express路由带参数