Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振文本表单验证未显示_Flutter_Dart - Fatal编程技术网

Flutter 颤振文本表单验证未显示

Flutter 颤振文本表单验证未显示,flutter,dart,Flutter,Dart,我看了一些关于如何使验证器工作的教程,但没有一个是有效的。有人能帮我吗?这是一个简单登录页面的代码。我的验证器不会显示在屏幕上,如果它应该检测到任何类型的错误。我看了一些教程,其中它显示为红色,但在我的应用程序中,它根本没有显示 class UserLogin extends StatefulWidget { UserLogin({this.auth,this.onSignedIn}); final BaseAuth auth; final VoidCallback onSign

我看了一些关于如何使验证器工作的教程,但没有一个是有效的。有人能帮我吗?这是一个简单登录页面的代码。我的验证器不会显示在屏幕上,如果它应该检测到任何类型的错误。我看了一些教程,其中它显示为红色,但在我的应用程序中,它根本没有显示

 class UserLogin extends StatefulWidget {

  UserLogin({this.auth,this.onSignedIn});
  final BaseAuth auth;
  final VoidCallback onSignedIn;
  @override
  State<StatefulWidget> createState()=> _UserLoginState();
}

class _UserLoginState extends State<UserLogin> {

  final formkey = GlobalKey<FormState>();

  bool _validateAndSave()
  {
    final form = formkey.currentState;
    if(form.validate())
      {
        form.save();
        return true;
      }
    else
      return false;
  }

   static final incorrect_icon = Icon(
     Icons.error,
     color: Colors.pink,
   );

   void _validateAndSubmit() async
   {

     if(_validateAndSave()) {
       try {
         String userId = await widget.auth.signIn(emailid, password);
         print('Signed in! $userId');
         //widget.onSignedIn();
         Navigator.push(context, MaterialPageRoute(builder: (context)=>Feed()));
       }
       catch (e) {
         print('Error: $e');
       }
     }

   }



   static final TextEditingController emailContr = new TextEditingController();
   static final TextEditingController passwordContr = new TextEditingController();

   static String get emailid => emailContr.text;
   static String get password => passwordContr.text;

   final _email = Container(
     padding: EdgeInsets.only(left: 10, right: 10),
     child: TextFormField(
       keyboardType: TextInputType.emailAddress,
       controller: emailContr,
       autofocus: false,
       validator: (input) {
         if(input.isEmpty)
           {
             return 'Email cannot be empty';
           }
          return null;
       },
       //onSaved: (input)=> emailid = input,
       decoration: InputDecoration(
         hintText: 'Enter Email Address',
         suffixIcon: Icon(Icons.email),
         border: OutlineInputBorder(
             borderRadius: BorderRadius.circular(10)
         ),
       ),
     ),
   );

   final _pass = Container(
     padding: EdgeInsets.only(left: 10, right: 10),
     child: TextFormField(
       controller: passwordContr,
       obscureText: true,
       autofocus: false,
       validator: (input) {
         if(input.length <= 6)
         {
           return 'Password should be at least 6 characters';
         }
         return null;
       },
       decoration: InputDecoration(
         hintText: 'Enter password',
         suffixIcon: Icon(Icons.lock),
         border: OutlineInputBorder(
             borderRadius: BorderRadius.circular(10)
         ),
       ),
     ),
   );

  /*final login_button =

    },
  );
   */

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.yellow,
      body: Container(
        child: Form(
          key: formkey,
          child: Column(
            children: <Widget>[
              SizedBox(height: 200,),
              Text('Vibing',
                style:TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 64,
                ),
              ),
              SizedBox(height: 100,),
              _email,
              SizedBox(height: 20,),
              _pass,
              SizedBox(height:30),
              RaisedButton(

                  color: Colors.yellow,
                  elevation: 5,
                  child: Text('Login'),
                  onPressed: (){
                    _validateAndSubmit();
                    formkey.currentState.reset();
                  }
              ),
              SizedBox(height:10),
              FlatButton(
                  child: Text('Forgot password'),
                  onPressed: ()=> Navigator.push(context, MaterialPageRoute(builder:(context)=>ForgotPassword()),)
              ),
              SizedBox(height:10),
              FlatButton(
                  child: Text('New? Register here!'),
                  onPressed: ()=> Navigator.push(context, MaterialPageRoute(builder:(context)=>UserReg()),)
              ),
            ],
          ),
        ),
        ) ,
      );
  }

}
类UserLogin扩展StatefulWidget{
UserLogin({this.auth,this.onSignedIn});
最终BaseAuth-auth;
最终作废已签字;
@凌驾
State createState()=>\u UserLoginState();
}
类_UserLoginState扩展状态{
final formkey=GlobalKey();
bool_validateAndSave()
{
最终形式=formkey.currentState;
if(form.validate())
{
form.save();
返回true;
}
其他的
返回false;
}
静态最终错误图标=图标(
错误,
颜色:颜色。粉红色,
);
void\u validateAndSubmit()异步
{
如果(_validateAndSave()){
试一试{
字符串userId=wait widget.auth.sign(emailid,密码);
打印('Signed in!$userId');
//widget.onSignedIn();
push(context,materialpage(builder:(context)=>Feed());
}
捕获(e){
打印('Error:$e');
}
}
}
静态最终文本编辑控制器emailContr=新文本编辑控制器();
静态最终TextEditingController密码控制=新建TextEditingController();
静态字符串get-emailid=>emailContr.text;
静态字符串get password=>passwordcontrol.text;
最终电子邮件=容器(
填充:仅限边设置(左:10,右:10),
子项:TextFormField(
键盘类型:TextInputType.emailAddress,
控制器:emailContr,
自动对焦:错误,
验证器:(输入){
if(input.isEmpty)
{
返回“电子邮件不能为空”;
}
返回null;
},
//onSaved:(输入)=>emailid=input,
装饰:输入装饰(
hintText:'输入电子邮件地址',
后缀:图标(Icons.email),
边框:大纲输入边框(
边界半径:边界半径。圆形(10)
),
),
),
);
最终通行证=集装箱(
填充:仅限边设置(左:10,右:10),
子项:TextFormField(
控制器:passwordContr,
蒙昧文字:对,
自动对焦:错误,
验证器:(输入){
if(input.length Navigator.push(上下文,MaterialPage路由(生成器:(上下文)=>ForgotPassword()),)
),
尺寸箱(高度:10),
扁平按钮(
孩子:文本('New?在这里注册!'),
onPressed:()=>Navigator.push(上下文,MaterialPage路由(生成器:(上下文)=>UserReg()),)
),
],
),
),
) ,
);
}
}

问题在于您在验证后重置表单,因此显示的任何错误都将重置。只需从登录按钮回调中删除此行:

formkey.currentState.reset();


问题是您在验证后重置表单,因此显示的任何错误都将重置。只需从登录按钮回调中删除此行:

formkey.currentState.reset();

reset():

将此[Form]的后代的每个[FormField]重置回其 [FormField.initialValue]

在您的情况下,initialValue是空字符串“”,这就是为什么当您调用
Form
reset()
方法时,它设置了一个空字符串,不会显示任何错误,因为没有任何错误。

reset():

将此[Form]的后代的每个[FormField]重置回其 [FormField.initialValue]

在您的例子中,initialValue是空字符串“”,这就是为什么当您调用
表单的
重置()
方法时,它设置了一个空字符串,不会显示任何错误,因为没有任何错误