1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/tiles/comment_tile.dart

122 lines
3.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';
2020-04-15 16:58:45 +00:00
import 'package:comunic/ui/widgets/like_widget.dart';
2019-05-16 12:52:22 +00:00
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-11-01 13:17:46 +00:00
import 'package:comunic/utils/ui_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 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,
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 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(),
2020-04-15 16:58:45 +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) => [
2020-04-15 16:58:45 +00:00
// Update comment content
PopupMenuItem(
enabled: comment.isOwner,
child: Text(tr("Update")),
value: _CommentAction.UPDATE,
),
2019-05-18 16:48:12 +00:00
2020-04-15 16:58:45 +00:00
// Delete comment
PopupMenuItem(
enabled: comment.isOwner,
child: Text(tr("Delete")),
value: _CommentAction.DELETE,
),
],
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,
2020-04-15 16:58:45 +00:00
style: TextStyle(
color: darkTheme() ? darkAccentColor : 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
2020-04-15 17:17:29 +00:00
LikeWidget(likeElement: comment),
2019-05-16 12:52:22 +00:00
],
);
}
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
}