cofounderella/lib/pages/register_page.dart

171 lines
5.3 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 {
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 {
showMsg(context, 'Password Mismatch',
'Password and repeated password do not match!');
2024-04-29 14:36:25 +02:00
}
}
2024-06-18 15:01:38 +02:00
bool _validateFields(BuildContext context) {
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',
);
return false;
}
return true;
}
2024-04-29 14:36:25 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface,
2024-06-18 15:01:38 +02:00
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
2024-06-17 17:57:32 +02:00
mainAxisAlignment: MainAxisAlignment.center,
children: [
2024-06-18 15:01:38 +02:00
const SizedBox(height: 25),
// logo and register message
2024-06-17 17:57:32 +02:00
Icon(
Icons.people_alt,
size: 60,
color: Theme.of(context).colorScheme.primary,
),
2024-06-18 15:01:38 +02:00
const SizedBox(height: 16),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Text(
textAlign: TextAlign.center,
'Create an account '
'and start connecting with potential co-founders.',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
fontSize: 16,
),
2024-06-17 17:57:32 +02:00
),
),
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-18 15:01:38 +02:00
// text fields
2024-06-17 17:57:32 +02:00
MyTextField(
2024-06-18 15:01:38 +02:00
hintText: 'First Name',
2024-06-17 17:57:32 +02:00
obscureText: false,
controller: _nameController,
),
MyTextField(
2024-06-18 15:01:38 +02:00
hintText: 'Last Name',
2024-06-17 17:57:32 +02:00
obscureText: false,
controller: _lastnameController,
),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
MyTextField(
2024-06-18 15:01:38 +02:00
hintText: 'E-Mail',
2024-06-17 17:57:32 +02:00
obscureText: false,
controller: _emailController,
),
2024-04-29 14:36:25 +02:00
2024-06-17 17:57:32 +02:00
MyTextField(
2024-06-18 15:01:38 +02:00
hintText: 'Password',
2024-06-17 17:57:32 +02:00
obscureText: true,
controller: _passwordController,
),
MyTextField(
2024-06-18 15:01:38 +02:00
hintText: 'Confirm Password',
2024-06-17 17:57:32 +02:00
obscureText: true,
controller: _confirmPassController,
),
2024-04-30 22:33:01 +02:00
2024-06-17 17:57:32 +02:00
const SizedBox(height: 25),
2024-06-18 15:01:38 +02:00
// register button
2024-06-17 17:57:32 +02:00
MyButton(
2024-06-18 15:01:38 +02:00
text: 'Register',
2024-06-17 17:57:32 +02:00
onTap: () => {
2024-06-18 15:01:38 +02:00
if (_validateFields(context)) {register(context)}
2024-06-17 17:57:32 +02:00
},
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-06-18 15:01:38 +02:00
),
2024-06-17 17:57:32 +02:00
),
2024-04-29 14:36:25 +02:00
),
);
}
}