2024-05-18 10:17:16 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-23 12:00:26 +00:00
|
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:get/get_utils/get_utils.dart';
|
2024-06-22 17:52:05 +00:00
|
|
|
import 'package:intl/intl.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/models/post.dart';
|
2024-05-25 05:19:16 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/widgets/account/account_avatar.dart';
|
2024-06-02 06:42:07 +00:00
|
|
|
import 'package:solian/widgets/account/account_profile_popup.dart';
|
2024-05-18 14:23:36 +00:00
|
|
|
import 'package:solian/widgets/attachments/attachment_list.dart';
|
2024-07-11 16:44:57 +00:00
|
|
|
import 'package:solian/widgets/markdown_text_content.dart';
|
2024-07-31 18:10:57 +00:00
|
|
|
import 'package:solian/widgets/posts/post_tags.dart';
|
2024-05-21 16:05:03 +00:00
|
|
|
import 'package:solian/widgets/posts/post_quick_action.dart';
|
2024-07-26 06:21:00 +00:00
|
|
|
import 'package:solian/widgets/sized_container.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:timeago/timeago.dart' show format;
|
|
|
|
|
|
|
|
class PostItem extends StatefulWidget {
|
|
|
|
final Post item;
|
2024-05-25 05:19:16 +00:00
|
|
|
final bool isClickable;
|
2024-05-22 12:56:10 +00:00
|
|
|
final bool isCompact;
|
2024-05-21 16:05:03 +00:00
|
|
|
final bool isReactable;
|
2024-05-25 05:19:16 +00:00
|
|
|
final bool isShowReply;
|
2024-05-25 09:21:27 +00:00
|
|
|
final bool isShowEmbed;
|
2024-06-22 17:52:05 +00:00
|
|
|
final bool isFullDate;
|
2024-07-13 10:54:08 +00:00
|
|
|
final bool isContentSelectable;
|
2024-05-25 16:11:00 +00:00
|
|
|
final String? overrideAttachmentParent;
|
2024-05-18 10:17:16 +00:00
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
const PostItem({
|
|
|
|
super.key,
|
|
|
|
required this.item,
|
2024-05-25 05:19:16 +00:00
|
|
|
this.isClickable = false,
|
2024-05-22 12:56:10 +00:00
|
|
|
this.isCompact = false,
|
2024-05-21 16:05:03 +00:00
|
|
|
this.isReactable = true,
|
2024-05-25 05:19:16 +00:00
|
|
|
this.isShowReply = true,
|
2024-05-25 09:21:27 +00:00
|
|
|
this.isShowEmbed = true,
|
2024-06-22 17:52:05 +00:00
|
|
|
this.isFullDate = false,
|
2024-07-13 10:54:08 +00:00
|
|
|
this.isContentSelectable = false,
|
2024-05-25 16:11:00 +00:00
|
|
|
this.overrideAttachmentParent,
|
2024-05-21 16:05:03 +00:00
|
|
|
});
|
2024-05-18 10:17:16 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<PostItem> createState() => _PostItemState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PostItemState extends State<PostItem> {
|
2024-05-19 12:30:50 +00:00
|
|
|
late final Post item;
|
|
|
|
|
2024-07-13 10:54:08 +00:00
|
|
|
Color get _unFocusColor =>
|
|
|
|
Theme.of(context).colorScheme.onSurface.withOpacity(0.75);
|
|
|
|
|
2024-05-19 12:30:50 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
item = widget.item;
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-07-25 17:16:32 +00:00
|
|
|
Widget _buildDate() {
|
2024-06-22 17:52:05 +00:00
|
|
|
if (widget.isFullDate) {
|
2024-07-30 06:49:26 +00:00
|
|
|
return Text(DateFormat('y/M/d HH:mm')
|
2024-07-25 17:16:32 +00:00
|
|
|
.format(item.publishedAt?.toLocal() ?? DateTime.now()));
|
2024-06-22 17:52:05 +00:00
|
|
|
} else {
|
2024-07-25 17:16:32 +00:00
|
|
|
return Text(
|
|
|
|
format(
|
|
|
|
item.publishedAt?.toLocal() ?? DateTime.now(),
|
|
|
|
locale: 'en_short',
|
|
|
|
),
|
|
|
|
);
|
2024-06-22 17:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 17:16:32 +00:00
|
|
|
Widget _buildHeader() {
|
2024-06-22 17:52:05 +00:00
|
|
|
return Row(
|
|
|
|
children: [
|
2024-06-26 16:33:49 +00:00
|
|
|
if (widget.isCompact)
|
|
|
|
AccountAvatar(
|
|
|
|
content: item.author.avatar.toString(),
|
|
|
|
radius: 10,
|
|
|
|
).paddingOnly(left: 2),
|
2024-07-30 06:49:26 +00:00
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
item.author.nick,
|
|
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
_buildDate().paddingOnly(left: 4),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
if (item.body['title'] != null)
|
|
|
|
Text(
|
|
|
|
item.body['title'],
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.bodyMedium!
|
|
|
|
.copyWith(fontSize: 15),
|
|
|
|
),
|
|
|
|
if (item.body['description'] != null)
|
|
|
|
Text(
|
|
|
|
item.body['description'],
|
|
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
|
|
),
|
|
|
|
if (item.body['description'] != null ||
|
|
|
|
item.body['title'] != null)
|
|
|
|
const Divider(thickness: 0.3, height: 1).paddingSymmetric(
|
|
|
|
vertical: 8,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
).paddingOnly(left: widget.isCompact ? 6 : 12),
|
|
|
|
),
|
2024-06-22 17:52:05 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-25 17:16:32 +00:00
|
|
|
Widget _buildFooter() {
|
2024-06-22 17:52:05 +00:00
|
|
|
List<String> labels = List.empty(growable: true);
|
2024-07-30 06:49:26 +00:00
|
|
|
if (widget.item.editedAt != null) {
|
2024-06-22 17:52:05 +00:00
|
|
|
labels.add('postEdited'.trParams({
|
2024-07-30 06:49:26 +00:00
|
|
|
'date': DateFormat('yy/M/d HH:mm').format(item.editedAt!.toLocal()),
|
2024-06-22 17:52:05 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (widget.item.realm != null) {
|
|
|
|
labels.add('postInRealm'.trParams({
|
2024-07-25 06:42:50 +00:00
|
|
|
'realm': widget.item.realm!.alias,
|
2024-06-22 17:52:05 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2024-07-08 11:56:03 +00:00
|
|
|
List<Widget> widgets = List.empty(growable: true);
|
|
|
|
|
|
|
|
if (widget.item.tags?.isNotEmpty ?? false) {
|
2024-07-31 18:10:57 +00:00
|
|
|
widgets.add(PostTagsList(tags: widget.item.tags!));
|
2024-07-08 11:56:03 +00:00
|
|
|
}
|
2024-06-22 17:52:05 +00:00
|
|
|
if (labels.isNotEmpty) {
|
2024-07-08 11:56:03 +00:00
|
|
|
widgets.add(Text(
|
2024-06-23 10:51:49 +00:00
|
|
|
labels.join(' · '),
|
2024-06-22 17:52:05 +00:00
|
|
|
textAlign: TextAlign.left,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 12,
|
2024-07-13 10:54:08 +00:00
|
|
|
color: _unFocusColor,
|
2024-06-22 17:52:05 +00:00
|
|
|
),
|
2024-07-08 11:56:03 +00:00
|
|
|
));
|
|
|
|
}
|
2024-07-26 10:23:51 +00:00
|
|
|
if (widget.item.pinnedAt != null) {
|
|
|
|
widgets.add(Text(
|
|
|
|
'postPinned'.tr,
|
|
|
|
style: TextStyle(fontSize: 12, color: _unFocusColor),
|
|
|
|
));
|
|
|
|
}
|
2024-07-08 11:56:03 +00:00
|
|
|
|
|
|
|
if (widgets.isEmpty) {
|
2024-06-22 17:52:05 +00:00
|
|
|
return const SizedBox();
|
2024-07-08 11:56:03 +00:00
|
|
|
} else {
|
|
|
|
return Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: widgets,
|
2024-07-09 13:23:38 +00:00
|
|
|
).paddingOnly(top: 4);
|
2024-06-22 17:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 17:16:32 +00:00
|
|
|
Widget _buildReply(BuildContext context) {
|
2024-05-22 12:56:10 +00:00
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
2024-05-23 12:00:26 +00:00
|
|
|
FaIcon(
|
|
|
|
FontAwesomeIcons.reply,
|
|
|
|
size: 16,
|
2024-07-13 10:54:08 +00:00
|
|
|
color: _unFocusColor,
|
2024-05-22 12:56:10 +00:00
|
|
|
),
|
2024-06-29 13:03:15 +00:00
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
'postRepliedNotify'.trParams(
|
|
|
|
{'username': '@${widget.item.replyTo!.author.name}'},
|
|
|
|
),
|
2024-07-13 10:54:08 +00:00
|
|
|
style: TextStyle(color: _unFocusColor),
|
2024-06-29 13:03:15 +00:00
|
|
|
).paddingOnly(left: 6),
|
|
|
|
),
|
2024-05-22 12:56:10 +00:00
|
|
|
],
|
|
|
|
).paddingOnly(left: 12),
|
|
|
|
Card(
|
|
|
|
elevation: 1,
|
|
|
|
child: PostItem(
|
|
|
|
item: widget.item.replyTo!,
|
|
|
|
isCompact: true,
|
2024-07-23 10:09:41 +00:00
|
|
|
overrideAttachmentParent: widget.item.id.toString(),
|
2024-05-22 12:56:10 +00:00
|
|
|
).paddingSymmetric(vertical: 8),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-25 17:16:32 +00:00
|
|
|
Widget _buildRepost(BuildContext context) {
|
2024-05-22 12:56:10 +00:00
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
2024-05-23 12:00:26 +00:00
|
|
|
FaIcon(
|
|
|
|
FontAwesomeIcons.retweet,
|
|
|
|
size: 16,
|
2024-07-13 10:54:08 +00:00
|
|
|
color: _unFocusColor,
|
2024-05-22 12:56:10 +00:00
|
|
|
),
|
2024-06-29 13:03:15 +00:00
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
'postRepostedNotify'.trParams(
|
|
|
|
{'username': '@${widget.item.repostTo!.author.name}'},
|
|
|
|
),
|
2024-07-13 10:54:08 +00:00
|
|
|
style: TextStyle(color: _unFocusColor),
|
2024-06-29 13:03:15 +00:00
|
|
|
).paddingOnly(left: 6),
|
|
|
|
),
|
2024-05-22 12:56:10 +00:00
|
|
|
],
|
|
|
|
).paddingOnly(left: 12),
|
|
|
|
Card(
|
|
|
|
elevation: 1,
|
|
|
|
child: PostItem(
|
|
|
|
item: widget.item.repostTo!,
|
|
|
|
isCompact: true,
|
2024-07-23 10:09:41 +00:00
|
|
|
overrideAttachmentParent: widget.item.id.toString(),
|
2024-05-22 12:56:10 +00:00
|
|
|
).paddingSymmetric(vertical: 8),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-18 10:17:16 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-07-25 08:08:46 +00:00
|
|
|
final List<int> attachments = item.body['attachments'] is List
|
|
|
|
? item.body['attachments']?.cast<int>()
|
|
|
|
: List.empty();
|
|
|
|
final hasAttachment = attachments.isNotEmpty;
|
2024-05-19 12:30:50 +00:00
|
|
|
|
2024-05-22 12:56:10 +00:00
|
|
|
if (widget.isCompact) {
|
|
|
|
return Column(
|
2024-06-22 17:52:05 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2024-05-22 12:56:10 +00:00
|
|
|
children: [
|
2024-07-25 17:16:32 +00:00
|
|
|
_buildHeader().paddingSymmetric(horizontal: 12),
|
2024-07-13 10:54:08 +00:00
|
|
|
MarkdownTextContent(
|
2024-07-23 10:09:41 +00:00
|
|
|
content: item.body['content'],
|
2024-07-13 10:54:08 +00:00
|
|
|
isSelectable: widget.isContentSelectable,
|
|
|
|
).paddingOnly(
|
2024-05-22 12:56:10 +00:00
|
|
|
left: 16,
|
|
|
|
right: 12,
|
|
|
|
top: 2,
|
|
|
|
bottom: hasAttachment ? 4 : 0,
|
|
|
|
),
|
2024-07-25 17:16:32 +00:00
|
|
|
_buildFooter().paddingOnly(left: 16),
|
2024-07-25 08:08:46 +00:00
|
|
|
if (attachments.isNotEmpty)
|
2024-07-13 10:54:08 +00:00
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.attachment,
|
|
|
|
size: 18,
|
|
|
|
color: _unFocusColor,
|
|
|
|
).paddingOnly(right: 6),
|
|
|
|
Text(
|
|
|
|
'postAttachmentTip'.trParams(
|
2024-07-25 08:08:46 +00:00
|
|
|
{'count': attachments.length.toString()},
|
2024-07-13 10:54:08 +00:00
|
|
|
),
|
|
|
|
style: TextStyle(color: _unFocusColor),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
).paddingOnly(left: 16, top: 4),
|
2024-05-22 12:56:10 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-18 14:23:36 +00:00
|
|
|
return Column(
|
2024-07-07 04:33:54 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2024-05-18 10:17:16 +00:00
|
|
|
children: [
|
2024-05-18 14:23:36 +00:00
|
|
|
Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2024-06-02 06:42:07 +00:00
|
|
|
GestureDetector(
|
|
|
|
child: AccountAvatar(content: item.author.avatar.toString()),
|
|
|
|
onTap: () {
|
|
|
|
showModalBottomSheet(
|
|
|
|
useRootNavigator: true,
|
|
|
|
isScrollControlled: true,
|
2024-06-03 15:36:46 +00:00
|
|
|
backgroundColor: Theme.of(context).colorScheme.surface,
|
2024-06-02 06:42:07 +00:00
|
|
|
context: context,
|
2024-06-03 15:36:46 +00:00
|
|
|
builder: (context) => AccountProfilePopup(
|
|
|
|
account: item.author,
|
|
|
|
),
|
2024-06-02 06:42:07 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-05-18 14:23:36 +00:00
|
|
|
Expanded(
|
|
|
|
child: Column(
|
2024-06-22 17:52:05 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2024-05-18 10:17:16 +00:00
|
|
|
children: [
|
2024-07-25 17:16:32 +00:00
|
|
|
_buildHeader(),
|
2024-07-26 06:21:00 +00:00
|
|
|
SizedContainer(
|
|
|
|
maxWidth: 640,
|
|
|
|
child: MarkdownTextContent(
|
|
|
|
content: item.body['content'],
|
|
|
|
isSelectable: widget.isContentSelectable,
|
|
|
|
).paddingOnly(left: 12, right: 8),
|
|
|
|
),
|
2024-05-25 09:21:27 +00:00
|
|
|
if (widget.item.replyTo != null && widget.isShowEmbed)
|
2024-05-25 05:19:16 +00:00
|
|
|
GestureDetector(
|
2024-07-25 17:16:32 +00:00
|
|
|
child: _buildReply(context).paddingOnly(top: 4),
|
2024-05-25 05:19:16 +00:00
|
|
|
onTap: () {
|
|
|
|
if (!widget.isClickable) return;
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'postDetail',
|
|
|
|
pathParameters: {
|
2024-07-23 10:09:41 +00:00
|
|
|
'id': widget.item.replyTo!.id.toString(),
|
2024-05-25 05:19:16 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-05-25 09:21:27 +00:00
|
|
|
if (widget.item.repostTo != null && widget.isShowEmbed)
|
2024-05-25 05:19:16 +00:00
|
|
|
GestureDetector(
|
2024-07-25 17:16:32 +00:00
|
|
|
child: _buildRepost(context).paddingOnly(top: 4),
|
2024-05-25 05:19:16 +00:00
|
|
|
onTap: () {
|
|
|
|
if (!widget.isClickable) return;
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'postDetail',
|
|
|
|
pathParameters: {
|
2024-07-23 10:09:41 +00:00
|
|
|
'alias': widget.item.repostTo!.id.toString(),
|
2024-05-25 05:19:16 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-07-25 17:16:32 +00:00
|
|
|
_buildFooter().paddingOnly(left: 12),
|
2024-05-18 10:17:16 +00:00
|
|
|
],
|
|
|
|
),
|
2024-07-31 17:21:27 +00:00
|
|
|
),
|
2024-05-18 14:23:36 +00:00
|
|
|
],
|
|
|
|
).paddingOnly(
|
2024-05-21 16:05:03 +00:00
|
|
|
top: 10,
|
2024-05-19 12:30:50 +00:00
|
|
|
bottom: hasAttachment ? 10 : 0,
|
2024-05-18 14:23:36 +00:00
|
|
|
right: 16,
|
|
|
|
left: 16,
|
|
|
|
),
|
2024-07-26 08:53:05 +00:00
|
|
|
AttachmentList(
|
|
|
|
parentId: widget.item.id.toString(),
|
|
|
|
attachmentsId: attachments,
|
|
|
|
isGrid: attachments.length > 1,
|
2024-05-25 16:11:00 +00:00
|
|
|
),
|
2024-07-11 16:44:57 +00:00
|
|
|
if (widget.isShowReply && widget.isReactable)
|
2024-07-09 15:06:55 +00:00
|
|
|
PostQuickAction(
|
|
|
|
isShowReply: widget.isShowReply,
|
|
|
|
isReactable: widget.isReactable,
|
|
|
|
item: widget.item,
|
|
|
|
onReact: (symbol, changes) {
|
|
|
|
setState(() {
|
2024-07-16 11:46:53 +00:00
|
|
|
item.metric!.reactionList[symbol] =
|
|
|
|
(item.metric!.reactionList[symbol] ?? 0) + changes;
|
2024-07-09 15:06:55 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
).paddingOnly(
|
|
|
|
top: hasAttachment ? 10 : 6,
|
|
|
|
left: hasAttachment ? 24 : 60,
|
|
|
|
right: 16,
|
|
|
|
bottom: 10,
|
|
|
|
)
|
|
|
|
else
|
|
|
|
const SizedBox(height: 10),
|
2024-05-18 10:17:16 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|