Browse Source

couleur page de login

master
Stephane 6 months ago
parent
commit
25abc65480
  1. 319
      lib/Views/loginPage.dart

319
lib/Views/loginPage.dart

@ -35,18 +35,6 @@ class _LoginPageState extends State<LoginPage> {
try { try {
final userCount = await AppDatabase.instance.getUserCount(); final userCount = await AppDatabase.instance.getUserCount();
print('Nombre d\'utilisateurs trouvés: $userCount'); // Debug 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) { } catch (error) {
print('Erreur lors de la vérification du nombre d\'utilisateurs: $error'); print('Erreur lors de la vérification du nombre d\'utilisateurs: $error');
setState(() { setState(() {
@ -65,11 +53,9 @@ class _LoginPageState extends State<LoginPage> {
Future<void> saveUserData(Users user, String role, int userId) async { Future<void> saveUserData(Users user, String role, int userId) async {
try { 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); userController.setUserWithCredentials(user, role, userId);
print(
print('Utilisateur sauvegardé: ${user.username}, rôle: $role, id: $userId'); 'Utilisateur sauvegardé: ${user.username}, rôle: $role, id: $userId');
} catch (error) { } catch (error) {
print('Erreur lors de la sauvegarde: $error'); print('Erreur lors de la sauvegarde: $error');
throw Exception('Erreur lors de la sauvegarde des données utilisateur'); throw Exception('Erreur lors de la sauvegarde des données utilisateur');
@ -82,10 +68,10 @@ class _LoginPageState extends State<LoginPage> {
final String username = _usernameController.text.trim(); final String username = _usernameController.text.trim();
final String password = _passwordController.text.trim(); final String password = _passwordController.text.trim();
// Validation basique
if (username.isEmpty || password.isEmpty) { if (username.isEmpty || password.isEmpty) {
setState(() { 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; _isErrorVisible = true;
}); });
return; return;
@ -97,45 +83,22 @@ class _LoginPageState extends State<LoginPage> {
}); });
try { try {
print('Tentative de connexion pour: $username');
// Vérification de la connexion à la base de données
final dbInstance = AppDatabase.instance; 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 // Vérifier les identifiants
bool isValidUser = await dbInstance.verifyUser(username, password); bool isValidUser = await dbInstance.verifyUser(username, password);
print('Résultat de la vérification: $isValidUser');
if (isValidUser) { if (isValidUser) {
// Récupérer les informations complètes de l'utilisateur
Users user = await dbInstance.getUser(username); Users user = await dbInstance.getUser(username);
print('Utilisateur récupéré: ${user.username}');
// Récupérer les credentials
Map<String, dynamic>? userCredentials = Map<String, dynamic>? userCredentials =
await dbInstance.getUserCredentials(username, password); await dbInstance.getUserCredentials(username, password);
if (userCredentials != null) { 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( await saveUserData(
user, user,
userCredentials['role'] as String, userCredentials['role'] as String,
userCredentials['id'] as int, userCredentials['id'] as int,
); );
// Navigation
if (mounted) { if (mounted) {
Navigator.pushReplacement( Navigator.pushReplacement(
context, context,
@ -146,145 +109,199 @@ class _LoginPageState extends State<LoginPage> {
throw Exception('Erreur lors de la récupération des credentials'); throw Exception('Erreur lors de la récupération des credentials');
} }
} else { } else {
print('Identifiants invalides pour: $username');
setState(() { setState(() {
_errorMessage = 'Nom d\'utilisateur ou mot de passe invalide'; _errorMessage = 'Nom d\'utilisateur ou mot de passe invalide';
_isErrorVisible = true; _isErrorVisible = true;
}); });
} }
} catch (error) { } catch (error) {
print('Erreur lors de la connexion: $error');
setState(() { setState(() {
_errorMessage = 'Erreur de connexion: ${error.toString()}'; _errorMessage = 'Erreur de connexion: ${error.toString()}';
_isErrorVisible = true; _isErrorVisible = true;
}); });
} finally { } finally {
if (mounted) { if (mounted) setState(() => _isLoading = false);
setState(() {
_isLoading = false;
});
}
} }
} }
@override @override
Widget build(BuildContext context) { 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( return Scaffold(
appBar: AppBar( backgroundColor: primaryColor,
title: const Text(
'Login',
style: TextStyle(color: Colors.white),
),
backgroundColor: const Color.fromARGB(255, 4, 54, 95),
centerTitle: true,
),
body: ParticleBackground( body: ParticleBackground(
child: Center( child: Center(
child: Container( child: SingleChildScrollView(
width: MediaQuery.of(context).size.width * 0.5, child: Container(
height: MediaQuery.of(context).size.height * 0.8, width: MediaQuery.of(context).size.width < 500
padding: const EdgeInsets.all(16.0), ? double.infinity
decoration: BoxDecoration( : 400,
color: Colors.white, padding:
shape: BoxShape.rectangle, const EdgeInsets.symmetric(horizontal: 24.0, vertical: 32.0),
borderRadius: BorderRadius.circular(30.0), decoration: BoxDecoration(
), color: cardColor.withOpacity(0.98),
child: Column( borderRadius: BorderRadius.circular(30.0),
crossAxisAlignment: CrossAxisAlignment.stretch, boxShadow: [
children: [ BoxShadow(
Container( color: primaryColor.withOpacity(0.2),
padding: const EdgeInsets.symmetric(vertical: 16.0), blurRadius: 16,
child: const Icon( spreadRadius: 4,
Icons.lock_outline, offset: const Offset(0, 8),
size: 100.0,
color: Color.fromARGB(255, 4, 54, 95),
), ),
), ],
TextField( ),
controller: _usernameController, child: Column(
enabled: !_isLoading, crossAxisAlignment: CrossAxisAlignment.stretch,
decoration: InputDecoration( children: [
labelText: 'Username', Center(
prefixIcon: const Icon(Icons.person, color: Colors.blueAccent), child: Column(
border: OutlineInputBorder( children: [
borderRadius: BorderRadius.circular(30.0), 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: 24),
const SizedBox(height: 16.0), TextField(
TextField( controller: _usernameController,
controller: _passwordController, enabled: !_isLoading,
enabled: !_isLoading, decoration: InputDecoration(
decoration: InputDecoration( labelText: 'Nom d\'utilisateur',
labelText: 'Password', labelStyle: TextStyle(
prefixIcon: const Icon(Icons.lock, color: Colors.redAccent), color: primaryColor.withOpacity(0.7),
border: OutlineInputBorder( ),
borderRadius: BorderRadius.circular(30.0), 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, const SizedBox(height: 18.0),
onSubmitted: (_) => _login(), TextField(
), controller: _passwordController,
const SizedBox(height: 16.0), enabled: !_isLoading,
Visibility( obscureText: true,
visible: _isErrorVisible, decoration: InputDecoration(
child: Text( labelText: 'Mot de passe',
_errorMessage, labelStyle: TextStyle(
style: const TextStyle( color: primaryColor.withOpacity(0.7),
color: Colors.red, ),
fontSize: 14, 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(),
), ),
), if (_isErrorVisible) ...[
const SizedBox(height: 16.0), const SizedBox(height: 12.0),
ElevatedButton( Text(
onPressed: _isLoading ? null : _login, _errorMessage,
style: ElevatedButton.styleFrom( style: const TextStyle(
backgroundColor: const Color(0xFF0015B7), color: Colors.redAccent,
elevation: 5.0, fontSize: 15,
shape: RoundedRectangleBorder( fontWeight: FontWeight.w600,
borderRadius: BorderRadius.circular(30.0), ),
textAlign: TextAlign.center,
), ),
minimumSize: const Size(double.infinity, 48), ],
), const SizedBox(height: 26.0),
child: _isLoading ElevatedButton(
? const SizedBox( onPressed: _isLoading ? null : _login,
height: 20, style: ElevatedButton.styleFrom(
width: 20, backgroundColor: accentColor,
child: CircularProgressIndicator( disabledBackgroundColor: accentColor.withOpacity(0.3),
color: Colors.white, foregroundColor: Colors.white,
strokeWidth: 2, elevation: 7.0,
), shape: RoundedRectangleBorder(
) borderRadius: BorderRadius.circular(30.0),
: const Text( ),
'Se connecter', minimumSize: const Size(double.infinity, 52),
style: TextStyle( ),
color: Colors.white, child: _isLoading
fontSize: 16, ? 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'),
),
],
],
),
), ),
), ),
), ),

Loading…
Cancel
Save