1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00
comunicmobile/lib/ui/tiles/comment_tile.dart

119 lines
3.0 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';
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({
Key? key,
required this.comment,
required this.user,
required this.onUpdateComment,
required this.onDeleteComment,
2022-03-11 15:40:56 +00:00
}) : super(key: key);
2019-05-16 12:52:22 +00:00
@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)!,
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,
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,
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(
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
}