95 lines
3.3 KiB
Dart
95 lines
3.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
|
class ProfilePictureWidget extends StatelessWidget {
|
|
final String? userId; // Modifié pour être nullable
|
|
final double radius;
|
|
final String? defaultImageUrl;
|
|
|
|
const ProfilePictureWidget({
|
|
super.key,
|
|
required this.userId,
|
|
this.radius = 25,
|
|
this.defaultImageUrl,
|
|
});
|
|
|
|
@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);
|
|
} else if (snapshot.hasError) {
|
|
print("Error loading profile: ${snapshot.error}");
|
|
return _buildDefaultAvatar(radius, defaultImageUrl);
|
|
} else if (snapshot.data != null && snapshot.data!.exists) {
|
|
final userData = snapshot.data!.data() as Map<String, dynamic>?;
|
|
final profilePhotoUrl = userData?['profilePhotoUrl'] as String?;
|
|
|
|
if (profilePhotoUrl != null && profilePhotoUrl.isNotEmpty) {
|
|
return CircleAvatar(
|
|
radius: radius,
|
|
backgroundImage: NetworkImage(profilePhotoUrl),
|
|
onBackgroundImageError: (e, stack) {
|
|
print("Error loading profile image: $e");
|
|
},
|
|
);
|
|
}
|
|
}
|
|
return _buildDefaultAvatar(radius, defaultImageUrl);
|
|
},
|
|
);
|
|
}
|
|
|
|
// Widget utilitaire pour construire un CircleAvatar de chargement
|
|
Widget _buildLoadingAvatar(double radius) {
|
|
return CircleAvatar(
|
|
radius: radius,
|
|
backgroundColor:
|
|
Colors.grey[300], // Couleur de fond pendant le chargement
|
|
child: SizedBox(
|
|
width: radius * 0.8, // Ajuster la taille du loader
|
|
height: radius * 0.8,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2), // Indicateur de chargement
|
|
),
|
|
);
|
|
}
|
|
|
|
// Widget utilitaire pour construire un CircleAvatar par défaut (avec icône ou image par défaut)
|
|
Widget _buildDefaultAvatar(double radius, String? defaultImageUrl) {
|
|
if (defaultImageUrl != null && defaultImageUrl.isNotEmpty) {
|
|
return CircleAvatar(
|
|
radius: radius,
|
|
// Utilisation de Image.network pour l'image par défaut, avec gestion d'erreur similaire
|
|
backgroundImage: Image.network(
|
|
defaultImageUrl,
|
|
errorBuilder: (context, error, stackTrace) {
|
|
print(
|
|
"Erreur de chargement Image.network pour l'URL par défaut: $defaultImageUrl, Erreur: $error");
|
|
return _buildIconAvatar(
|
|
radius); // Si l'image par défaut ne charge pas, afficher l'icône
|
|
},
|
|
).image, // .image pour ImageProvider
|
|
);
|
|
} else {
|
|
return _buildIconAvatar(
|
|
radius); // Si pas d'URL par défaut fournie, afficher l'icône
|
|
}
|
|
}
|
|
|
|
// Widget utilitaire pour construire un CircleAvatar avec une icône par défaut
|
|
Widget _buildIconAvatar(double radius) {
|
|
return CircleAvatar(
|
|
radius: radius,
|
|
child: Icon(Icons.account_circle, size: radius * 1.5), // Icône par défaut
|
|
);
|
|
}
|
|
}
|