From 25abc65480eeea02b4aefbe1e69ea57e2e747267 Mon Sep 17 00:00:00 2001 From: Stephane Date: Sat, 31 May 2025 00:45:16 +0300 Subject: [PATCH] couleur page de login --- lib/Views/loginPage.dart | 325 ++++++++++++++++++++------------------- 1 file changed, 171 insertions(+), 154 deletions(-) diff --git a/lib/Views/loginPage.dart b/lib/Views/loginPage.dart index 3ab2505..6466d58 100644 --- a/lib/Views/loginPage.dart +++ b/lib/Views/loginPage.dart @@ -35,18 +35,6 @@ class _LoginPageState extends State { try { final userCount = await AppDatabase.instance.getUserCount(); print('Nombre d\'utilisateurs trouvés: $userCount'); // Debug - - // Commentez cette partie pour permettre le login même sans utilisateurs - /* - if (userCount == 0) { - if (mounted) { - Navigator.pushReplacement( - context, - MaterialPageRoute(builder: (context) => const AccueilPage()), - ); - } - } - */ } catch (error) { print('Erreur lors de la vérification du nombre d\'utilisateurs: $error'); setState(() { @@ -65,11 +53,9 @@ class _LoginPageState extends State { Future saveUserData(Users user, String role, int userId) async { try { - // ✅ CORRECTION : Utiliser la nouvelle méthode du contrôleur - // Le contrôleur se charge maintenant de tout (observable + SharedPreferences) userController.setUserWithCredentials(user, role, userId); - - print('Utilisateur sauvegardé: ${user.username}, rôle: $role, id: $userId'); + print( + 'Utilisateur sauvegardé: ${user.username}, rôle: $role, id: $userId'); } catch (error) { print('Erreur lors de la sauvegarde: $error'); throw Exception('Erreur lors de la sauvegarde des données utilisateur'); @@ -82,10 +68,10 @@ class _LoginPageState extends State { final String username = _usernameController.text.trim(); final String password = _passwordController.text.trim(); - // Validation basique if (username.isEmpty || password.isEmpty) { setState(() { - _errorMessage = 'Veuillez saisir le nom d\'utilisateur et le mot de passe'; + _errorMessage = + 'Veuillez saisir le nom d\'utilisateur et le mot de passe'; _isErrorVisible = true; }); return; @@ -97,45 +83,22 @@ class _LoginPageState extends State { }); try { - print('Tentative de connexion pour: $username'); - - // Vérification de la connexion à la base de données final dbInstance = AppDatabase.instance; - - // Test de connexion à la base - try { - final userCount = await dbInstance.getUserCount(); - print('Base de données accessible, $userCount utilisateurs trouvés'); - } catch (dbError) { - throw Exception('Impossible d\'accéder à la base de données: $dbError'); - } // Vérifier les identifiants bool isValidUser = await dbInstance.verifyUser(username, password); - print('Résultat de la vérification: $isValidUser'); - + if (isValidUser) { - // Récupérer les informations complètes de l'utilisateur Users user = await dbInstance.getUser(username); - print('Utilisateur récupéré: ${user.username}'); - - // Récupérer les credentials - Map? userCredentials = + Map? userCredentials = await dbInstance.getUserCredentials(username, password); if (userCredentials != null) { - print('Connexion réussie pour: ${user.username}'); - print('Rôle: ${userCredentials['role']}'); - print('ID: ${userCredentials['id']}'); - - // ✅ CORRECTION : Sauvegarder ET mettre à jour le contrôleur await saveUserData( user, userCredentials['role'] as String, userCredentials['id'] as int, ); - - // Navigation if (mounted) { Navigator.pushReplacement( context, @@ -146,149 +109,203 @@ class _LoginPageState extends State { throw Exception('Erreur lors de la récupération des credentials'); } } else { - print('Identifiants invalides pour: $username'); setState(() { _errorMessage = 'Nom d\'utilisateur ou mot de passe invalide'; _isErrorVisible = true; }); } } catch (error) { - print('Erreur lors de la connexion: $error'); setState(() { _errorMessage = 'Erreur de connexion: ${error.toString()}'; _isErrorVisible = true; }); } finally { - if (mounted) { - setState(() { - _isLoading = false; - }); - } + if (mounted) setState(() => _isLoading = false); } } @override Widget build(BuildContext context) { + final Color primaryBlue = const Color(0xFF0033A1); + final Color accentRed = const Color(0xFFD70000); + final Color secondaryBlue = const Color(0xFF1976D2); + final Color primaryColor = primaryBlue; + final Color accentColor = secondaryBlue; + final Color cardColor = Colors.white; + return Scaffold( - appBar: AppBar( - title: const Text( - 'Login', - style: TextStyle(color: Colors.white), - ), - backgroundColor: const Color.fromARGB(255, 4, 54, 95), - centerTitle: true, - ), + backgroundColor: primaryColor, body: ParticleBackground( child: Center( - child: Container( - width: MediaQuery.of(context).size.width * 0.5, - height: MediaQuery.of(context).size.height * 0.8, - padding: const EdgeInsets.all(16.0), - decoration: BoxDecoration( - color: Colors.white, - shape: BoxShape.rectangle, - borderRadius: BorderRadius.circular(30.0), - ), - child: Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - Container( - padding: const EdgeInsets.symmetric(vertical: 16.0), - child: const Icon( - Icons.lock_outline, - size: 100.0, - color: Color.fromARGB(255, 4, 54, 95), + child: SingleChildScrollView( + child: Container( + width: MediaQuery.of(context).size.width < 500 + ? double.infinity + : 400, + padding: + const EdgeInsets.symmetric(horizontal: 24.0, vertical: 32.0), + decoration: BoxDecoration( + color: cardColor.withOpacity(0.98), + borderRadius: BorderRadius.circular(30.0), + boxShadow: [ + BoxShadow( + color: primaryColor.withOpacity(0.2), + blurRadius: 16, + spreadRadius: 4, + offset: const Offset(0, 8), ), - ), - TextField( - controller: _usernameController, - enabled: !_isLoading, - decoration: InputDecoration( - labelText: 'Username', - prefixIcon: const Icon(Icons.person, color: Colors.blueAccent), - border: OutlineInputBorder( - borderRadius: BorderRadius.circular(30.0), + ], + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Center( + child: Column( + children: [ + CircleAvatar( + radius: 38, + backgroundColor: accentColor.withOpacity(0.15), + child: Icon( + Icons.lock_outline, + color: accentColor, + size: 50, + ), + ), + const SizedBox(height: 14), + Text( + 'GUYCOM', + style: TextStyle( + color: primaryColor, + fontWeight: FontWeight.bold, + fontSize: 28, + ), + ), + const SizedBox(height: 4), + Text( + 'Connectez-vous à votre compte', + style: TextStyle( + color: primaryColor.withOpacity(.8), + fontSize: 16, + ), + ), + ], ), ), - ), - const SizedBox(height: 16.0), - TextField( - controller: _passwordController, - enabled: !_isLoading, - decoration: InputDecoration( - labelText: 'Password', - prefixIcon: const Icon(Icons.lock, color: Colors.redAccent), - border: OutlineInputBorder( - borderRadius: BorderRadius.circular(30.0), + const SizedBox(height: 24), + TextField( + controller: _usernameController, + enabled: !_isLoading, + decoration: InputDecoration( + labelText: 'Nom d\'utilisateur', + labelStyle: TextStyle( + color: primaryColor.withOpacity(0.7), + ), + prefixIcon: Icon(Icons.person, color: accentColor), + filled: true, + fillColor: accentColor.withOpacity(0.045), + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(30.0), + borderSide: BorderSide(color: accentColor, width: 2), + ), + focusedBorder: OutlineInputBorder( + borderRadius: BorderRadius.circular(30.0), + borderSide: BorderSide(color: accentColor, width: 2), + ), ), ), - obscureText: true, - onSubmitted: (_) => _login(), - ), - const SizedBox(height: 16.0), - Visibility( - visible: _isErrorVisible, - child: Text( - _errorMessage, - style: const TextStyle( - color: Colors.red, - fontSize: 14, + const SizedBox(height: 18.0), + TextField( + controller: _passwordController, + enabled: !_isLoading, + obscureText: true, + decoration: InputDecoration( + labelText: 'Mot de passe', + labelStyle: TextStyle( + color: primaryColor.withOpacity(0.7), + ), + prefixIcon: Icon(Icons.lock, color: accentColor), + filled: true, + fillColor: accentColor.withOpacity(0.045), + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(30.0), + ), + focusedBorder: OutlineInputBorder( + borderRadius: BorderRadius.circular(30.0), + borderSide: BorderSide(color: accentColor, width: 2), + ), ), - textAlign: TextAlign.center, + onSubmitted: (_) => _login(), ), - ), - const SizedBox(height: 16.0), - ElevatedButton( - onPressed: _isLoading ? null : _login, - style: ElevatedButton.styleFrom( - backgroundColor: const Color(0xFF0015B7), - elevation: 5.0, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(30.0), + if (_isErrorVisible) ...[ + const SizedBox(height: 12.0), + Text( + _errorMessage, + style: const TextStyle( + color: Colors.redAccent, + fontSize: 15, + fontWeight: FontWeight.w600, + ), + textAlign: TextAlign.center, ), - minimumSize: const Size(double.infinity, 48), - ), - child: _isLoading - ? const SizedBox( - height: 20, - width: 20, - child: CircularProgressIndicator( - color: Colors.white, - strokeWidth: 2, - ), - ) - : const Text( - 'Se connecter', - style: TextStyle( - color: Colors.white, - fontSize: 16, + ], + const SizedBox(height: 26.0), + ElevatedButton( + onPressed: _isLoading ? null : _login, + style: ElevatedButton.styleFrom( + backgroundColor: accentColor, + disabledBackgroundColor: accentColor.withOpacity(0.3), + foregroundColor: Colors.white, + elevation: 7.0, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(30.0), + ), + minimumSize: const Size(double.infinity, 52), + ), + child: _isLoading + ? const SizedBox( + height: 24, + width: 24, + child: CircularProgressIndicator( + color: Colors.white, + strokeWidth: 2.5, + ), + ) + : const Text( + 'Se connecter', + style: TextStyle( + color: Colors.white, + fontSize: 18, + fontWeight: FontWeight.bold, + letterSpacing: .4, + ), ), - ), - ), - // Bouton de debug (à supprimer en production) - if (_isErrorVisible) - TextButton( - onPressed: () async { - try { - final count = await AppDatabase.instance.getUserCount(); - print('Debug: $count utilisateurs dans la base'); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('$count utilisateurs trouvés')), - ); - } catch (e) { - print('Debug error: $e'); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Erreur: $e')), - ); - } - }, - child: const Text('Debug: Vérifier BDD'), ), - ], + // Option debug, à enlever en prod + if (_isErrorVisible) ...[ + TextButton( + onPressed: () async { + try { + final count = + await AppDatabase.instance.getUserCount(); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('$count utilisateurs trouvés')), + ); + } catch (e) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Erreur: $e')), + ); + } + }, + child: const Text('Debug: Vérifier BDD'), + ), + ], + ], + ), ), ), ), ), ); } -} \ No newline at end of file +}