cofounderella/lib/pages/register_page.dart

170 lines
5.2 KiB
Dart
Raw Normal View History

2024-04-29 14:36:25 +02:00
import 'package:flutter/material.dart';
2024-05-18 00:24:10 +02:00
import 'package:firebase_auth/firebase_auth.dart';
2024-04-29 14:36:25 +02:00
import '../components/my_button.dart';
import '../components/my_textfield.dart';
2024-06-14 14:28:59 +02:00
import '../utils/helper_dialogs.dart';
2024-05-18 00:24:10 +02:00
import '../services/auth/auth_service.dart';
2024-05-19 17:01:06 +02:00
import '../services/user_service.dart';
2024-04-29 14:36:25 +02:00
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
2024-05-18 00:24:10 +02:00
Future<void> register(BuildContext context) async {
final auth = AuthService();
2024-04-29 14:36:25 +02:00
// check if passwords match
if (_passwordController.text == _confirmPassController.text) {
try {
2024-05-19 17:01:06 +02:00
await auth
.signUpWithEmailPassword(
_emailController.text, _passwordController.text)
.then((userCredential) {
2024-06-02 16:32:19 +02:00
UserService.saveUserRegistrationData(
userCredential,
_emailController.text,
_nameController.text,
_lastnameController.text);
2024-05-19 17:01:06 +02:00
});
2024-05-18 00:24:10 +02:00
} on FirebaseAuthException catch (e) {
if (context.mounted) {
showMsg(
context,
e.code.isNotEmpty ? e.code : 'Registration error',
'${e.message}',
);
}
2024-04-29 14:36:25 +02:00
}
} else {
2024-05-18 00:24:10 +02:00
showMsg(context, 'Registration error', 'Passwords do not match!');
2024-04-29 14:36:25 +02:00
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface,
2024-04-29 14:36:25 +02:00
body: Center(
2024-06-17 17:57:32 +02:00
child: ListView(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// logo
Icon(
Icons.people_alt,
size: 60,
color: Theme.of(context).colorScheme.primary,
),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
const SizedBox(height: 50),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
// register message
Text(
"Let's create an account for you",
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
fontSize: 16,
),
),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
const SizedBox(height: 25),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
// name text field
MyTextField(
hintText: "First Name",
obscureText: false,
controller: _nameController,
),
MyTextField(
hintText: "Last Name",
obscureText: false,
controller: _lastnameController,
),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
// email text field
MyTextField(
hintText: "E-Mail",
obscureText: false,
controller: _emailController,
),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
const SizedBox(height: 10),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
// password text field
MyTextField(
hintText: "Password",
obscureText: true,
controller: _passwordController,
),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
MyTextField(
hintText: "Confirm Password",
obscureText: true,
controller: _confirmPassController,
),
2024-04-30 22:33:01 +02:00
2024-06-17 17:57:32 +02:00
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)}
},
2024-04-30 22:33:01 +02:00
),
2024-06-17 17:57:32 +02:00
const SizedBox(height: 25),
// register now
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Already have an account? ',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
2024-04-30 22:33:01 +02:00
),
2024-06-17 17:57:32 +02:00
GestureDetector(
onTap: onTap,
child: const Text(
'Login now',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
],
)
2024-04-30 22:33:01 +02:00
],
2024-06-17 17:57:32 +02:00
),
],
),
2024-04-29 14:36:25 +02:00
),
);
}
}