You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
5.7 KiB
187 lines
5.7 KiB
import 'package:flutter/material.dart';
|
|
import 'package:youmazgestion/Models/users.dart';
|
|
import 'package:youmazgestion/accueil.dart';
|
|
|
|
import '../Services/authDatabase.dart';
|
|
|
|
class RegistrationPage extends StatefulWidget {
|
|
const RegistrationPage({super.key});
|
|
|
|
@override
|
|
_RegistrationPageState createState() => _RegistrationPageState();
|
|
}
|
|
|
|
class _RegistrationPageState extends State<RegistrationPage> {
|
|
late TextEditingController _nameController;
|
|
late TextEditingController _lastNameController;
|
|
late TextEditingController _emailController;
|
|
late TextEditingController _usernameController;
|
|
late TextEditingController _passwordController;
|
|
String _selectedRole = 'user'; // Default role is 'user'
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_nameController = TextEditingController();
|
|
_lastNameController = TextEditingController();
|
|
_emailController = TextEditingController();
|
|
_usernameController = TextEditingController();
|
|
_passwordController = TextEditingController();
|
|
AuthDatabase.instance.initDatabase();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameController.dispose();
|
|
_lastNameController.dispose();
|
|
_emailController.dispose();
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _register() {
|
|
// Get the entered user information
|
|
final String name = _nameController.text;
|
|
final String lastName = _lastNameController.text;
|
|
final String email = _emailController.text;
|
|
final String username = _usernameController.text;
|
|
final String password = _passwordController.text;
|
|
final String role = _selectedRole;
|
|
|
|
// Create a new user object
|
|
final Users user = Users(
|
|
id: 0, // The id will be assigned automatically by the database
|
|
name: name,
|
|
lastName: lastName,
|
|
email: email,
|
|
password: password,
|
|
username: username,
|
|
role: role,
|
|
);
|
|
|
|
// Save the user in the database
|
|
AuthDatabase.instance.createUser(user).then((value) {
|
|
// Registration successful
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: const Text('Registration Successful'),
|
|
content: const Text('You have successfully registered.'),
|
|
actions: <Widget>[
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
// Navigate to the login page
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const AccueilPage()),
|
|
);
|
|
},
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}).catchError((error) {
|
|
print(error);
|
|
// Registration failed
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: const Text('Registration Failed'),
|
|
content: const Text('An error occurred during registration.'),
|
|
actions: <Widget>[
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Registration'),
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
TextField(
|
|
controller: _nameController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'First Name',
|
|
),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
TextField(
|
|
controller: _lastNameController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Last Name',
|
|
),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
TextField(
|
|
controller: _emailController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
),
|
|
keyboardType: TextInputType.emailAddress,
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
TextField(
|
|
controller: _usernameController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Username',
|
|
),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
TextField(
|
|
controller: _passwordController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Password',
|
|
),
|
|
obscureText: true,
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
DropdownButton<String>(
|
|
value: _selectedRole,
|
|
onChanged: (String? newValue) {
|
|
setState(() {
|
|
_selectedRole = newValue!;
|
|
});
|
|
},
|
|
items: <String>['admin', 'user']
|
|
.map<DropdownMenuItem<String>>((String value) {
|
|
return DropdownMenuItem<String>(
|
|
value: value,
|
|
child: Text(value),
|
|
);
|
|
}).toList(),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
ElevatedButton(
|
|
onPressed: _register,
|
|
child: const Text('Register'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|