140 lines
4.8 KiB
Dart
140 lines
4.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import '../../../core/auth/auth_provider.dart';
|
|
import '../../../core/router/app_router.dart';
|
|
import '../../../core/theme/app_colors.dart';
|
|
import 'auth_widgets.dart';
|
|
|
|
/// Écran d'inscription utilisateur.
|
|
class RegisterScreen extends ConsumerStatefulWidget {
|
|
const RegisterScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<RegisterScreen> createState() => _RegisterScreenState();
|
|
}
|
|
|
|
class _RegisterScreenState extends ConsumerState<RegisterScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _firstNameController = TextEditingController();
|
|
final _lastNameController = TextEditingController();
|
|
final _emailController = TextEditingController();
|
|
final _phoneController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_firstNameController.dispose();
|
|
_lastNameController.dispose();
|
|
_emailController.dispose();
|
|
_phoneController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
|
|
final success = await ref.read(authProvider.notifier).register(
|
|
firstName: _firstNameController.text.trim(),
|
|
lastName: _lastNameController.text.trim(),
|
|
email: _emailController.text.trim(),
|
|
password: _passwordController.text,
|
|
phone: _phoneController.text.trim().isEmpty
|
|
? null
|
|
: _phoneController.text.trim(),
|
|
);
|
|
|
|
if (success && mounted) {
|
|
context.go(AppRoutes.home);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final authState = ref.watch(authProvider);
|
|
|
|
return AuthScaffold(
|
|
showBackButton: true,
|
|
child: AuthFormCard(
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const AuthHeader(subtitle: 'Créez votre compte en quelques secondes'),
|
|
TextFormField(
|
|
controller: _firstNameController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Prénom',
|
|
prefixIcon: Icon(Icons.person_outline_rounded),
|
|
),
|
|
validator: (v) => v == null || v.isEmpty ? 'Prénom requis' : null,
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _lastNameController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Nom',
|
|
prefixIcon: Icon(Icons.badge_outlined),
|
|
),
|
|
validator: (v) => v == null || v.isEmpty ? 'Nom requis' : null,
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _emailController,
|
|
keyboardType: TextInputType.emailAddress,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
prefixIcon: Icon(Icons.email_outlined),
|
|
),
|
|
validator: (v) => v == null || v.isEmpty ? 'Email requis' : null,
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _phoneController,
|
|
keyboardType: TextInputType.phone,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Téléphone (optionnel)',
|
|
prefixIcon: Icon(Icons.phone_outlined),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Mot de passe',
|
|
prefixIcon: Icon(Icons.lock_outline),
|
|
),
|
|
validator: (v) {
|
|
if (v == null || v.length < 8) {
|
|
return 'Minimum 8 caractères';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
if (authState.error != null) ...[
|
|
const SizedBox(height: 12),
|
|
Text(authState.error!, style: const TextStyle(color: AppColors.error)),
|
|
],
|
|
const SizedBox(height: 24),
|
|
ElevatedButton(
|
|
onPressed: authState.isLoading ? null : _submit,
|
|
child: authState.isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
|
|
)
|
|
: const Text('S\'inscrire'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|