import 'package:comunic/models/comment.dart'; import 'package:comunic/models/user.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/network_image_widget.dart'; import 'package:comunic/utils/date_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:flutter/material.dart'; /// Single comment tile /// /// @author Pierre HUBERT enum _CommentAction { DELETE, UPDATE } class CommentTile extends StatelessWidget { final Comment comment; final User user; final void Function(Comment) onUpdateLike; final void Function(Comment) onUpdateComment; final void Function(Comment) onDeleteComment; const CommentTile({ Key key, @required this.comment, @required this.user, @required this.onUpdateLike, @required this.onUpdateComment, @required this.onDeleteComment, }) : assert(comment != null), assert(user != null), assert(onUpdateLike != null), assert(onUpdateComment != null), assert(onDeleteComment != null), super(key: key); @override Widget build(BuildContext context) { return ListTile( leading: _buildAccountImageWidget(), title: Text( user.displayName, ), subtitle: _buildCommentContent(), trailing: Text(diffTimeFromNowToStr(comment.timeSent), style: TextStyle(fontSize: 10.0),), ); } Widget _buildAccountImageWidget() { return PopupMenuButton<_CommentAction>( child: AccountImageWidget( user: user, ), onSelected: _selectedMenuOption, itemBuilder: (c) => [ // Update comment content PopupMenuItem( enabled: comment.isOwner, child: Text(tr("Update")), value: _CommentAction.UPDATE, ), // Delete comment PopupMenuItem( enabled: comment.isOwner, child: Text(tr("Delete")), value: _CommentAction.DELETE, ), ], ); } Widget _buildCommentContent() { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // Comment image Container( child: comment.hasImage ? NetworkImageWidget( url: comment.imageURL, allowFullScreen: true, height: 100.0, width: null, ) : null, ), // Comment text Container( child: comment.hasContent ? Text( comment.content, style: TextStyle(color: Colors.black), ) : null, ), // Comment likes _buildLikeButton(), ], ); } String get _likeString { if (comment.likes == 0) return tr("Like"); if (comment.likes == 1) return tr("1 Like"); else return tr("%num% likes", args: {"num": comment.likes.toString()}); } /// Build like button associated to this post Widget _buildLikeButton() { return Padding( padding: const EdgeInsets.only(top: 4.0, bottom: 4.0), child: Align( alignment: AlignmentDirectional.topStart, child: Column( children: [ InkWell( onTap: () => onUpdateLike(comment), child: Row( children: [ Icon( Icons.thumb_up, color: comment.userLike ? Colors.blue : null, size: 15.0, ), SizedBox( width: 8.0, ), Text(_likeString), ], ), ), ], ), ), ); } /// A menu option has been selected void _selectedMenuOption(_CommentAction value) { switch (value) { // Update comment content case _CommentAction.UPDATE: onUpdateComment(comment); break; // Delete comment case _CommentAction.DELETE: onDeleteComment(comment); break; } } }