以下是代码行:
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"),
),
],
);
},
);
}
}
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"),
),
],
);
},
);
}
}