162 lines
5.5 KiB
Dart
162 lines
5.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class CommentCardWidget extends StatelessWidget {
|
|
final String comment;
|
|
final String authorName;
|
|
final String authorImageUrl;
|
|
final DateTime publishDate;
|
|
final int likesCount;
|
|
final int otherRepliesCount;
|
|
final bool isLiked;
|
|
final VoidCallback? onLike;
|
|
final VoidCallback? onReply;
|
|
final VoidCallback? onViewOtherReplies;
|
|
|
|
const CommentCardWidget({
|
|
super.key,
|
|
required this.comment,
|
|
required this.authorName,
|
|
required this.authorImageUrl,
|
|
required this.publishDate,
|
|
this.likesCount = 0,
|
|
this.otherRepliesCount = 0,
|
|
this.isLiked = false,
|
|
this.onLike,
|
|
this.onReply,
|
|
this.onViewOtherReplies,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
CircleAvatar(
|
|
radius: 18,
|
|
backgroundImage:
|
|
authorImageUrl.isNotEmpty ? NetworkImage(authorImageUrl) : null,
|
|
onBackgroundImageError: (_, __) {},
|
|
child: authorImageUrl.isEmpty
|
|
? const Icon(Icons.person, size: 18)
|
|
: null,
|
|
),
|
|
const SizedBox(width: 10),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Flexible(
|
|
child: Text(
|
|
_firstName(authorName),
|
|
overflow: TextOverflow.ellipsis,
|
|
style:
|
|
Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
fontWeight: FontWeight.w600,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
Text(
|
|
_formatDate(publishDate),
|
|
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 6),
|
|
Text(
|
|
comment,
|
|
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
|
color: Colors.white,
|
|
height: 1.35,
|
|
),
|
|
),
|
|
const SizedBox(height: 6),
|
|
Row(
|
|
children: [
|
|
TextButton.icon(
|
|
onPressed: onLike,
|
|
style: TextButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
minimumSize: const Size(0, 32),
|
|
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
),
|
|
icon: Icon(
|
|
isLiked ? Icons.favorite : Icons.favorite_outline,
|
|
size: 18,
|
|
color: isLiked ? Colors.redAccent : Colors.grey,
|
|
),
|
|
label: Text(
|
|
likesCount > 0 ? '$likesCount' : 'Like',
|
|
style: const TextStyle(color: Colors.grey),
|
|
),
|
|
),
|
|
const SizedBox(width: 4),
|
|
TextButton(
|
|
onPressed: onReply,
|
|
style: TextButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
minimumSize: const Size(0, 32),
|
|
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
),
|
|
child: const Text(
|
|
'Répondre',
|
|
style: TextStyle(color: Colors.grey),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
if (otherRepliesCount > 0)
|
|
TextButton(
|
|
onPressed: onViewOtherReplies,
|
|
style: TextButton.styleFrom(
|
|
padding: EdgeInsets.zero,
|
|
minimumSize: const Size(0, 28),
|
|
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
),
|
|
child: Text(
|
|
'Voir $otherRepliesCount autres réponses',
|
|
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
|
color: Colors.blueGrey[200],
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
String _firstName(String fullName) {
|
|
final trimmed = fullName.trim();
|
|
if (trimmed.isEmpty) return '';
|
|
return trimmed.split(RegExp(r'\s+')).first;
|
|
}
|
|
|
|
String _formatDate(DateTime date) {
|
|
final now = DateTime.now();
|
|
final difference = now.difference(date);
|
|
|
|
if (difference.inDays > 7) {
|
|
return '${date.day}/${date.month}/${date.year}';
|
|
} else if (difference.inDays > 0) {
|
|
return '${difference.inDays} jour${difference.inDays > 1 ? 's' : ''}';
|
|
} else if (difference.inHours > 0) {
|
|
return '${difference.inHours}h';
|
|
} else if (difference.inMinutes > 0) {
|
|
return '${difference.inMinutes}min';
|
|
} else {
|
|
return 'À l\'instant';
|
|
}
|
|
}
|
|
}
|