cofounderella/lib/pages/register_page.dart

170 lines
5.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import '../components/my_button.dart';
import '../components/my_textfield.dart';
import '../utils/helper_dialogs.dart';
import '../services/auth/auth_service.dart';
import '../services/user_service.dart';
class RegisterPage extends StatelessWidget {
//text controllers
final TextEditingController _nameController = TextEditingController();
final TextEditingController _lastnameController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _confirmPassController = TextEditingController();
// tap to go to login page
final void Function()? onTap;
RegisterPage({super.key, required this.onTap});
// register method
Future<void> register(BuildContext context) async {
final auth = AuthService();
// check if passwords match
if (_passwordController.text == _confirmPassController.text) {
try {
await auth
.signUpWithEmailPassword(
_emailController.text, _passwordController.text)
.then((userCredential) {
UserService.saveUserRegistrationData(
userCredential,
_emailController.text,
_nameController.text,
_lastnameController.text);
});
} on FirebaseAuthException catch (e) {
if (context.mounted) {
showMsg(
context,
e.code.isNotEmpty ? e.code : 'Registration error',
'${e.message}',
);
}
}
} else {
showMsg(context, 'Registration error', 'Passwords do not match!');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface,
body: Center(
child: ListView(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// logo
Icon(
Icons.people_alt,
size: 60,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(height: 50),
// register message
Text(
"Let's create an account for you",
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
fontSize: 16,
),
),
const SizedBox(height: 25),
// name text field
MyTextField(
hintText: "First Name",
obscureText: false,
controller: _nameController,
),
MyTextField(
hintText: "Last Name",
obscureText: false,
controller: _lastnameController,
),
// email text field
MyTextField(
hintText: "E-Mail",
obscureText: false,
controller: _emailController,
),
const SizedBox(height: 10),
// password text field
MyTextField(
hintText: "Password",
obscureText: true,
controller: _passwordController,
),
MyTextField(
hintText: "Confirm Password",
obscureText: true,
controller: _confirmPassController,
),
const SizedBox(height: 25),
//login button
MyButton(
text: "Register",
onTap: () => {
if ((_nameController.text.trim().isEmpty &&
_lastnameController.text.trim().isEmpty) ||
_emailController.text.trim().isEmpty ||
_passwordController.text.trim().isEmpty)
{
showMsg(
context,
'Missing information',
'Please enter your name, email and password',
)
}
else
{register(context)}
},
),
const SizedBox(height: 25),
// register now
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Already have an account? ',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
GestureDetector(
onTap: onTap,
child: const Text(
'Login now',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
],
)
],
),
],
),
),
);
}
}