import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import '../../services/auth_service.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _formKey = GlobalKey(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); bool _isLoading = false; @override void dispose() { _emailController.dispose(); _passwordController.dispose(); super.dispose(); } Future _login() async { if (_formKey.currentState!.validate()) { setState(() => _isLoading = true); try { await AuthService.login( _emailController.text, _passwordController.text, ); if (mounted) { context.go('/home'); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Erreur lors de la connexion: $e'), backgroundColor: Colors.red, ), ); } } finally { if (mounted) { setState(() => _isLoading = false); } } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Connexion'), ), body: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ TextFormField( controller: _emailController, decoration: const InputDecoration( labelText: 'Email', border: OutlineInputBorder(), ), keyboardType: TextInputType.emailAddress, validator: (value) { if (value == null || value.isEmpty) { return 'Veuillez entrer votre email'; } if (!value.contains('@')) { return 'Veuillez entrer un email valide'; } return null; }, ), const SizedBox(height: 16), TextFormField( controller: _passwordController, decoration: const InputDecoration( labelText: 'Mot de passe', border: OutlineInputBorder(), ), obscureText: true, validator: (value) { if (value == null || value.isEmpty) { return 'Veuillez entrer votre mot de passe'; } return null; }, ), const SizedBox(height: 24), ElevatedButton( onPressed: _isLoading ? null : _login, child: _isLoading ? const CircularProgressIndicator() : const Text('Se connecter'), ), const SizedBox(height: 16), TextButton( onPressed: () => context.go('/register'), child: const Text('Créer un compte'), ), const SizedBox(height: 8), TextButton( onPressed: () => context.go('/parent-register'), child: const Text('Créer un compte parent'), ), ], ), ), ), ), ); } }