1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-30 00:36:28 +00:00
comunicmobile/lib/ui/tiles/comment_tile.dart

157 lines
4.1 KiB
Dart
Raw Normal View History

2019-05-16 12:52:22 +00:00
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';
2019-05-18 16:50:15 +00:00
import 'package:comunic/utils/date_utils.dart';
2019-05-18 13:54:10 +00:00
import 'package:comunic/utils/intl_utils.dart';
2019-05-16 12:52:22 +00:00
import 'package:flutter/material.dart';
/// Single comment tile
///
/// @author Pierre HUBERT
2019-05-18 16:48:12 +00:00
enum _CommentAction { DELETE, UPDATE }
2019-05-18 14:48:19 +00:00
2019-05-16 12:52:22 +00:00
class CommentTile extends StatelessWidget {
final Comment comment;
final User user;
2019-05-18 13:54:10 +00:00
final void Function(Comment) onUpdateLike;
2019-05-18 16:48:12 +00:00
final void Function(Comment) onUpdateComment;
2019-05-18 14:48:19 +00:00
final void Function(Comment) onDeleteComment;
2019-05-16 12:52:22 +00:00
2019-05-18 13:54:10 +00:00
const CommentTile({
Key key,
@required this.comment,
@required this.user,
@required this.onUpdateLike,
2019-05-18 16:48:12 +00:00
@required this.onUpdateComment,
2019-05-18 14:48:19 +00:00
@required this.onDeleteComment,
2019-05-18 13:54:10 +00:00
}) : assert(comment != null),
2019-05-16 12:52:22 +00:00
assert(user != null),
2019-05-18 13:54:10 +00:00
assert(onUpdateLike != null),
2019-05-18 16:48:12 +00:00
assert(onUpdateComment != null),
assert(onDeleteComment != null),
2019-05-16 12:52:22 +00:00
super(key: key);
@override
Widget build(BuildContext context) {
return ListTile(
2019-05-18 14:48:19 +00:00
leading: _buildAccountImageWidget(),
2019-05-18 13:54:10 +00:00
title: Text(
user.displayName,
),
2019-05-16 12:52:22 +00:00
subtitle: _buildCommentContent(),
2019-05-18 16:50:15 +00:00
trailing: Text(diffTimeFromNowToStr(comment.timeSent), style: TextStyle(fontSize: 10.0),),
2019-05-16 12:52:22 +00:00
);
}
2019-05-18 14:48:19 +00:00
Widget _buildAccountImageWidget() {
return PopupMenuButton<_CommentAction>(
child: AccountImageWidget(
user: user,
),
onSelected: _selectedMenuOption,
itemBuilder: (c) => [
2019-05-18 16:48:12 +00:00
// Update comment content
2019-05-18 14:48:19 +00:00
PopupMenuItem(
enabled: comment.isOwner,
2019-05-18 16:48:12 +00:00
child: Text(tr("Update")),
value: _CommentAction.UPDATE,
),
// Delete comment
PopupMenuItem(
enabled: comment.isOwner,
child: Text(tr("Delete")),
2019-05-18 14:48:19 +00:00
value: _CommentAction.DELETE,
2019-05-18 16:48:12 +00:00
),
2019-05-18 14:48:19 +00:00
],
);
}
2019-05-16 12:52:22 +00:00
Widget _buildCommentContent() {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
// Comment image
Container(
child: comment.hasImage
? NetworkImageWidget(
url: comment.imageURL,
allowFullScreen: true,
height: 100.0,
2019-05-18 13:54:10 +00:00
width: null,
2019-05-16 12:52:22 +00:00
)
: null,
),
// Comment text
Container(
2019-05-18 13:54:10 +00:00
child: comment.hasContent
? Text(
comment.content,
2019-05-18 14:22:40 +00:00
style: TextStyle(color: Colors.black),
2019-05-18 13:54:10 +00:00
)
: null,
2019-05-16 12:52:22 +00:00
),
2019-05-18 13:54:10 +00:00
// Comment likes
_buildLikeButton(),
2019-05-16 12:52:22 +00:00
],
);
}
2019-05-18 13:54:10 +00:00
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() {
2019-05-18 14:22:40 +00:00
return Padding(
padding: const EdgeInsets.only(top: 4.0, bottom: 4.0),
child: Align(
alignment: AlignmentDirectional.topStart,
child: Column(
2019-05-18 13:54:10 +00:00
children: <Widget>[
2019-05-18 14:22:40 +00:00
InkWell(
onTap: () => onUpdateLike(comment),
child: Row(
children: <Widget>[
Icon(
Icons.thumb_up,
color: comment.userLike ? Colors.blue : null,
size: 15.0,
),
SizedBox(
width: 8.0,
),
Text(_likeString),
],
),
2019-05-18 13:54:10 +00:00
),
],
),
),
);
}
2019-05-18 14:48:19 +00:00
/// A menu option has been selected
void _selectedMenuOption(_CommentAction value) {
switch (value) {
2019-05-18 16:48:12 +00:00
// Update comment content
case _CommentAction.UPDATE:
onUpdateComment(comment);
break;
2019-05-18 14:48:19 +00:00
// Delete comment
case _CommentAction.DELETE:
onDeleteComment(comment);
break;
}
}
2019-05-16 12:52:22 +00:00
}