我无法正确设置验证器来注册并登录flutter项目

问题描述 投票:0回答:1
我已将 TextFormFiled 小部件移动到单独的类和样式中。理论上,我正确地编写了验证器的逻辑,但由于某种原因它不起作用,在单击“开始”按钮后,如果该行为空,则应该出现有关此错误,并且如果未指定邮件正确,那么密码是一样的,类似,但是为什么点击按钮后没有任何反应

以下是代码行:

class _SignUpPageState extends State<SignUpPage> { final FirebaseAuthServices _auth = FirebaseAuthServices(); final _formKey = GlobalKey<FormState>(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); final _confirmPasswordController = TextEditingController(); String? validateEmail(String? email) { RegExp emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$'); final isEmailValid = emailRegex.hasMatch(email ?? ''); if (!isEmailValid) { return "Please enter a valid email"; } return null; } @override void dispose() { _emailController.dispose(); _passwordController.dispose(); _confirmPasswordController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.grey[300], body: Stack( children: [ Image.asset( "assets/images/back_signup.png", fit: BoxFit.cover, width: double.infinity, height: double.infinity, ), Center( child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.end, children: [ Text( "Please enter your details", textAlign: TextAlign.center, style: TextStyle( color: Colors.white, fontSize: 38, fontFamily: "Montserrat", fontWeight: FontWeight.bold, ), ), SizedBox(height: 22), CustomTextField( textColor: Colors.grey, hintText: "Email", controller: _emailController, obscureText: false, width: 326, validator: validateEmail, ), SizedBox(height: 18), CustomTextField( textColor: Colors.white, controller: _passwordController, hintText: "Password", obscureText: true, width: 326, validator: (password) => password!.length < 6 ? "Password must be at least 6 characters long" : null, ), SizedBox(height: 18), CustomTextField( textColor: Colors.white, controller: _confirmPasswordController, hintText: "Confirm Password", obscureText: true, width: 326, validator: (password) => password!.length < 6 ? "Password must be at least 6 characters long" : null, ), SizedBox(height: 18), SizedBox( width: 326, height: 45, child: ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { _signUp(); } }, style: ElevatedButton.styleFrom( backgroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(30), ), ), child: Text( "Get Started", style: TextStyle( color: Colors.black, fontSize: 16, fontFamily: "Montserrat", fontWeight: FontWeight.bold, ), ), ), ), SizedBox(height: 24), ], ), ), ), ], ), ); } void _signUp() async { String email = _emailController.text; String password = _passwordController.text; String confirmPassword = _confirmPasswordController.text; if (password != confirmPassword) { _showErrorDialog("Passwords do not match"); return; } User? user = await _auth.signUpWithEmailAndPassword(email, password); if (user != null) { print("User created: ${user.email}"); Navigator.push( context, MaterialPageRoute( builder: (context) => const HomePage(), ), ); } else { _showErrorDialog("Failed to create user"); } } void _showErrorDialog(String message) { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text("Error"), content: Text(message), actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); }, child: Text("OK"), ), ], ); }, ); } }
    
android ios flutter dart mobile
1个回答
0
投票
这是您的工作代码的固定版本。

class _SignUpPageState extends State<SignUpPage> { final _formKey = GlobalKey<FormState>(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); final _confirmPasswordController = TextEditingController(); String? validateEmail(String? email) { RegExp emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$'); final isEmailValid = emailRegex.hasMatch(email ?? ''); if (!isEmailValid) { return "Please enter a valid email"; } return null; } @override void dispose() { _emailController.dispose(); _passwordController.dispose(); _confirmPasswordController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.grey[300], body: Stack( children: [ Image.asset( "assets/images/back_signup.png", fit: BoxFit.cover, width: double.infinity, height: double.infinity, ), Center( child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.end, children: [ Text( "Please enter your details", textAlign: TextAlign.center, style: TextStyle( color: Colors.white, fontSize: 38, fontFamily: "Montserrat", fontWeight: FontWeight.bold, ), ), SizedBox(height: 22), CustomTextField( textColor: Colors.grey, hintText: "Email", controller: _emailController, obscureText: false, width: 326, validator: validateEmail, ), SizedBox(height: 18), CustomTextField( textColor: Colors.white, controller: _passwordController, hintText: "Password", obscureText: true, width: 326, validator: (password) => password!.length < 6 ? "Password must be at least 6 characters long" : null, ), SizedBox(height: 18), CustomTextField( textColor: Colors.white, controller: _confirmPasswordController, hintText: "Confirm Password", obscureText: true, width: 326, validator: (password) => password!.length < 6 ? "Password must be at least 6 characters long" : null, ), SizedBox(height: 18), SizedBox( width: 326, height: 45, child: ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { _signUp(); } }, style: ElevatedButton.styleFrom( backgroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(30), ), ), child: Text( "Get Started", style: TextStyle( color: Colors.black, fontSize: 16, fontFamily: "Montserrat", fontWeight: FontWeight.bold, ), ), ), ), SizedBox(height: 24), ], ), ), ), ], ), ); } Widget CustomTextField({ required Color textColor, required double width, required String hintText, required TextEditingController controller, required bool obscureText, required String? Function(String?) validator, }) { return TextFormField( controller: controller, decoration: InputDecoration( labelText: hintText, hintText: hintText, ), validator: validator, obscureText: obscureText, ); } void _signUp() async { String email = _emailController.text; String password = _passwordController.text; String confirmPassword = _confirmPasswordController.text; if (password != confirmPassword) { _showErrorDialog("Passwords do not match"); return; } User? user = await _auth.signUpWithEmailAndPassword(email, password); if (user != null) { print("User created: ${user.email}"); Navigator.push( context, MaterialPageRoute( builder: (context) => const HomePage(), ), ); } else { _showErrorDialog("Failed to create user"); } } void _showErrorDialog(String message) { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text("Error"), content: Text(message), actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); }, child: Text("OK"), ), ], ); }, ); } }
    
© www.soinside.com 2019 - 2024. All rights reserved.