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';
|
2020-04-16 08:14:45 +00:00
|
|
|
import 'package:comunic/ui/widgets/text_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({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.comment,
|
|
|
|
required this.user,
|
|
|
|
required this.onUpdateComment,
|
|
|
|
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(
|
2022-03-10 18:39:57 +00:00
|
|
|
diffTimeFromNowToStr(comment.timeSent)!,
|
2020-04-15 16:58:45 +00:00
|
|
|
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,
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Update")!),
|
2020-04-15 16:58:45 +00:00
|
|
|
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,
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Delete")!),
|
2020-04-15 16:58:45 +00:00
|
|
|
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(
|
2022-03-10 18:39:57 +00:00
|
|
|
url: comment.imageURL!,
|
2019-05-16 12:52:22 +00:00
|
|
|
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
|
2020-04-16 08:14:45 +00:00
|
|
|
? TextWidget(
|
|
|
|
content: 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
|
|
|
}
|