import 'package:flutter/material.dart'; import 'package:afterwork/data/datasources/user_remote_data_source.dart'; import 'package:afterwork/data/models/user_model.dart'; import 'package:http/http.dart' as http; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override _LoginScreenState createState() => _LoginScreenState(); } class _LoginScreenState extends State { final _formKey = GlobalKey(); String _email = ''; // Initialisation par défaut String _password = ''; // Initialisation par défaut bool _isPasswordVisible = false; final UserRemoteDataSource _userRemoteDataSource = UserRemoteDataSource(http.Client()); void _togglePasswordVisibility() { setState(() { _isPasswordVisible = !_isPasswordVisible; }); } void _submit() async { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); print('Email: $_email'); // Pour vérifier la valeur de l'email print('Mot de passe: $_password'); // Pour vérifier la valeur du mot de passe if (_email.isEmpty || _password.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Veuillez remplir tous les champs')), ); return; } try { UserModel user = await _userRemoteDataSource.authenticateUser(_email, _password); print('Connexion réussie : ${user.email}'); // Naviguer vers la page d'accueil ou autre } catch (e) { print('Erreur lors de la connexion: $e'); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(e.toString())), ); } } else { print('Formulaire invalide'); } } @override Widget build(BuildContext context) { final size = MediaQuery.of(context).size; return Scaffold( backgroundColor: Colors.white, body: Stack( children: [ // Arrière-plan Positioned.fill( child: Image.asset( 'lib/assets/images/background.webp', // Remplace par le chemin de ton image fit: BoxFit.cover, ), ), // Contenu de la page Center( child: SingleChildScrollView( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Image.asset( 'lib/assets/images/logo.png', height: size.height * 0.2, ), const SizedBox(height: 20), const Text( 'Bienvenue sur AfterWork', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: Colors.blueAccent, ), ), const SizedBox(height: 40), TextFormField( decoration: const InputDecoration( labelText: 'Email', border: OutlineInputBorder(), prefixIcon: Icon(Icons.email), ), keyboardType: TextInputType.emailAddress, validator: (value) { if (value == null || value.isEmpty) { return 'Veuillez entrer votre email'; } if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) { return 'Veuillez entrer un email valide'; } return null; }, onSaved: (value) { _email = value ?? ''; // Utiliser une chaîne vide si value est null }, ), const SizedBox(height: 20), TextFormField( decoration: InputDecoration( labelText: 'Mot de passe', border: const OutlineInputBorder(), prefixIcon: const Icon(Icons.lock), suffixIcon: IconButton( icon: Icon( _isPasswordVisible ? Icons.visibility : Icons.visibility_off, ), onPressed: _togglePasswordVisibility, ), ), obscureText: !_isPasswordVisible, validator: (value) { if (value == null || value.isEmpty) { return 'Veuillez entrer votre mot de passe'; } if (value.length < 6) { return 'Le mot de passe doit comporter au moins 6 caractères'; } return null; }, onSaved: (value) { _password = value ?? ''; // Utiliser une chaîne vide si value est null }, ), const SizedBox(height: 20), SizedBox( width: double.infinity, child: ElevatedButton( style: ElevatedButton.styleFrom( padding: const EdgeInsets.all(16.0), textStyle: const TextStyle(fontSize: 18), backgroundColor: Colors.blueAccent, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), ), onPressed: _submit, child: const Text('Connexion'), ), ), const SizedBox(height: 20), TextButton( onPressed: () { // Naviguer vers la page d'inscription }, child: const Text( 'Pas encore de compte ? Inscrivez-vous', style: TextStyle(color: Colors.blueAccent), ), ), ], ), ), ), ), ], ), ); } }