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);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user