Début refacto en MVVM (Login, drawer OK
This commit is contained in:
@ -2,9 +2,9 @@ import 'package:flutter/material.dart';
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
|
||||
class ProfilePictureWidget extends StatelessWidget {
|
||||
final String userId;
|
||||
final String? userId; // Modifié pour être nullable
|
||||
final double radius;
|
||||
final String? defaultImageUrl; // URL de l'image par défaut (optionnel)
|
||||
final String? defaultImageUrl;
|
||||
|
||||
const ProfilePictureWidget({
|
||||
super.key,
|
||||
@ -15,42 +15,34 @@ class ProfilePictureWidget extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Vérifier si userId est null ou vide
|
||||
if (userId == null || userId!.isEmpty) {
|
||||
return _buildDefaultAvatar(radius, defaultImageUrl);
|
||||
}
|
||||
|
||||
return FutureBuilder<DocumentSnapshot>(
|
||||
future: FirebaseFirestore.instance.collection('users').doc(userId).get(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return _buildLoadingAvatar(
|
||||
radius); // Afficher un avatar de chargement
|
||||
return _buildLoadingAvatar(radius);
|
||||
} else if (snapshot.hasError) {
|
||||
print("Erreur FutureBuilder ProfilePictureWidget: ${snapshot.error}");
|
||||
return _buildDefaultAvatar(radius,
|
||||
defaultImageUrl); // Afficher avatar par défaut en cas d'erreur Firestore
|
||||
print("Error loading profile: ${snapshot.error}");
|
||||
return _buildDefaultAvatar(radius, defaultImageUrl);
|
||||
} else if (snapshot.data != null && snapshot.data!.exists) {
|
||||
final userData = snapshot.data!;
|
||||
final profilePhotoUrl = userData['profilePhotoUrl'] as String?;
|
||||
final userData = snapshot.data!.data() as Map<String, dynamic>?;
|
||||
final profilePhotoUrl = userData?['profilePhotoUrl'] as String?;
|
||||
|
||||
if (profilePhotoUrl != null && profilePhotoUrl.isNotEmpty) {
|
||||
return CircleAvatar(
|
||||
radius: radius,
|
||||
// Utilisation de Image.network directement dans backgroundImage
|
||||
backgroundImage: Image.network(
|
||||
profilePhotoUrl,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
print(
|
||||
"Erreur de chargement Image.network pour l'URL: $profilePhotoUrl, Erreur: $error");
|
||||
// En cas d'erreur de chargement de l'image réseau, afficher l'avatar par défaut
|
||||
return _buildDefaultAvatar(radius, defaultImageUrl);
|
||||
},
|
||||
).image, // .image pour obtenir un ImageProvider pour backgroundImage
|
||||
backgroundImage: NetworkImage(profilePhotoUrl),
|
||||
onBackgroundImageError: (e, stack) {
|
||||
print("Error loading profile image: $e");
|
||||
},
|
||||
);
|
||||
} else {
|
||||
return _buildDefaultAvatar(radius,
|
||||
defaultImageUrl); // Pas d'URL dans Firestore, afficher avatar par défaut
|
||||
}
|
||||
} else {
|
||||
return _buildDefaultAvatar(radius,
|
||||
defaultImageUrl); // Document utilisateur non trouvé ou n'existe pas
|
||||
}
|
||||
return _buildDefaultAvatar(radius, defaultImageUrl);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -1,128 +1,136 @@
|
||||
import 'package:em2rp/providers/user_provider.dart';
|
||||
import 'package:em2rp/providers/local_auth_provider.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/views/calendar_page.dart';
|
||||
import 'package:em2rp/views/my_account_page.dart';
|
||||
import 'package:em2rp/views/user_management_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:em2rp/views/widgets/image/profile_picture.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class MainDrawer extends StatelessWidget {
|
||||
final String currentPage;
|
||||
final UserProvider userProvider;
|
||||
|
||||
const MainDrawer(
|
||||
{super.key, required this.currentPage, required this.userProvider});
|
||||
const MainDrawer({super.key, required this.currentPage});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Drawer(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: <Widget>[
|
||||
DrawerHeader(
|
||||
// Header du drawer
|
||||
decoration: BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/EM2_NsurB.jpg'),
|
||||
fit: BoxFit.cover,
|
||||
colorFilter: ColorFilter.mode(
|
||||
AppColors.noir.withOpacity(0.4),
|
||||
BlendMode.darken,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
alignment: Alignment.bottomLeft,
|
||||
child: Column(
|
||||
// Use Column to arrange logo and user name
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
ProfilePictureWidget(
|
||||
userId: userProvider.uid!,
|
||||
radius: 30,
|
||||
return Consumer<LocalAuthProvider>(
|
||||
builder: (context, userProvider, child) {
|
||||
final hasUser = userProvider.currentUser != null;
|
||||
|
||||
return Drawer(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: <Widget>[
|
||||
DrawerHeader(
|
||||
decoration: BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/EM2_NsurB.jpg'),
|
||||
fit: BoxFit.cover,
|
||||
colorFilter: ColorFilter.mode(
|
||||
AppColors.noir.withOpacity(0.4),
|
||||
BlendMode.darken,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Bonjour, ${userProvider.firstName ?? 'Erreur'}',
|
||||
style: TextStyle(
|
||||
color: AppColors.blanc,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
shadows: [
|
||||
Shadow(
|
||||
blurRadius: 3.0,
|
||||
color: AppColors.noir,
|
||||
offset: Offset(1.0, 1.0),
|
||||
),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
alignment: Alignment.bottomLeft,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
if (hasUser)
|
||||
ProfilePictureWidget(
|
||||
userId: userProvider.currentUser!.uid,
|
||||
radius: 30,
|
||||
)
|
||||
else
|
||||
CircleAvatar(
|
||||
radius: 30,
|
||||
child: Icon(Icons.account_circle, size: 45),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
hasUser
|
||||
? 'Bonjour, ${userProvider.currentUser!.firstName}'
|
||||
: 'Bonjour, Utilisateur',
|
||||
style: TextStyle(
|
||||
color: AppColors.blanc,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
shadows: [
|
||||
Shadow(
|
||||
blurRadius: 3.0,
|
||||
color: AppColors.noir,
|
||||
offset: Offset(1.0, 1.0),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
// Lien vers la page Calendrier
|
||||
leading: const Icon(Icons.calendar_today),
|
||||
title: const Text('Calendrier'),
|
||||
selected: currentPage == '/calendar',
|
||||
selectedColor: AppColors.rouge,
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.pushReplacement(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => CalendarPage()),
|
||||
);
|
||||
},
|
||||
),
|
||||
ExpansionTileTheme(
|
||||
data: const ExpansionTileThemeData(
|
||||
iconColor: AppColors.noir,
|
||||
collapsedIconColor: AppColors.noir,
|
||||
),
|
||||
child: ExpansionTile(
|
||||
leading: const Icon(Icons.settings),
|
||||
title: const Text('Paramètres'),
|
||||
children: <Widget>[
|
||||
ListTile(
|
||||
leading: const Icon(Icons.account_circle),
|
||||
// Lien vers "Mon Compte"
|
||||
title: const Text('Mon Compte'),
|
||||
selected: currentPage == '/my_account',
|
||||
selectedColor: AppColors.rouge,
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.pushReplacement(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const MyAccountPage()),
|
||||
);
|
||||
},
|
||||
ListTile(
|
||||
leading: const Icon(Icons.calendar_today),
|
||||
title: const Text('Calendrier'),
|
||||
selected: currentPage == '/calendar',
|
||||
selectedColor: AppColors.rouge,
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.pushReplacement(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => CalendarPage()),
|
||||
);
|
||||
},
|
||||
),
|
||||
ExpansionTileTheme(
|
||||
data: const ExpansionTileThemeData(
|
||||
iconColor: AppColors.noir,
|
||||
collapsedIconColor: AppColors.noir,
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.group),
|
||||
// Lien vers "Gestion des Utilisateurs"
|
||||
title: const Text('Gestion des Utilisateurs'),
|
||||
selected: currentPage ==
|
||||
'/user_management', // Check if current page is UserManagementPage
|
||||
selectedColor: AppColors.rouge,
|
||||
onTap: () {
|
||||
Navigator.pop(context); // Ferme le drawer
|
||||
Navigator.pushReplacement(
|
||||
// Navigue vers UserManagementPage
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const UserManagementPage()),
|
||||
);
|
||||
},
|
||||
child: ExpansionTile(
|
||||
leading: const Icon(Icons.settings),
|
||||
title: const Text('Paramètres'),
|
||||
children: <Widget>[
|
||||
ListTile(
|
||||
leading: const Icon(Icons.account_circle),
|
||||
title: const Text('Mon Compte'),
|
||||
selected: currentPage == '/my_account',
|
||||
selectedColor: AppColors.rouge,
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.pushReplacement(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const MyAccountPage()),
|
||||
);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.group),
|
||||
title: const Text('Gestion des Utilisateurs'),
|
||||
selected: currentPage == '/user_management',
|
||||
selectedColor: AppColors.rouge,
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.pushReplacement(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const UserManagementPage()),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user