Solian/lib/widgets/posts/post_item.dart

666 lines
22 KiB
Dart
Raw Normal View History

2024-08-01 20:42:38 +00:00
import 'package:animations/animations.dart';
2024-05-18 10:17:16 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_animate/flutter_animate.dart';
2024-05-23 12:00:26 +00:00
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:gap/gap.dart';
2024-09-16 15:35:44 +00:00
import 'package:get/get.dart';
import 'package:intl/intl.dart';
2024-05-18 10:17:16 +00:00
import 'package:solian/models/post.dart';
2024-09-16 15:35:44 +00:00
import 'package:solian/providers/content/posts.dart';
2024-10-12 17:31:59 +00:00
import 'package:solian/router.dart';
2024-08-01 20:42:38 +00:00
import 'package:solian/screens/posts/post_detail.dart';
import 'package:solian/shells/title_shell.dart';
import 'package:solian/theme.dart';
2024-05-18 10:17:16 +00:00
import 'package:solian/widgets/account/account_avatar.dart';
2024-05-18 14:23:36 +00:00
import 'package:solian/widgets/attachments/attachment_list.dart';
import 'package:solian/widgets/link_expansion.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';
import 'package:solian/widgets/posts/post_quick_action.dart';
2024-09-23 14:43:02 +00:00
import 'package:solian/widgets/relative_date.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;
final bool isCompact;
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-08-04 13:12:35 +00:00
final bool isOverrideEmbedClickable;
final bool isFullDate;
final bool isContentSelectable;
2024-09-16 15:35:44 +00:00
final bool showFeaturedReply;
2024-08-01 21:10:10 +00:00
final String? attachmentParent;
2024-10-12 17:31:59 +00:00
final EdgeInsets? padding;
2024-10-11 16:41:03 +00:00
final Function? onComment;
2024-05-18 10:17:16 +00:00
const PostItem({
super.key,
required this.item,
2024-05-25 05:19:16 +00:00
this.isClickable = false,
this.isCompact = false,
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-08-04 13:12:35 +00:00
this.isOverrideEmbedClickable = false,
this.isFullDate = false,
this.isContentSelectable = false,
2024-09-16 15:35:44 +00:00
this.showFeaturedReply = false,
2024-08-01 21:10:10 +00:00
this.attachmentParent,
2024-10-12 17:31:59 +00:00
this.padding,
2024-10-11 16:41:03 +00:00
this.onComment,
});
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;
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-09-23 14:43:02 +00:00
@override
Widget build(BuildContext context) {
final List<String> attachments = item.body['attachments'] is List
? List.from(item.body['attachments']?.whereType<String>())
: List.empty();
final hasAttachment = attachments.isNotEmpty;
2024-07-08 11:56:03 +00:00
2024-09-23 14:43:02 +00:00
if (widget.isCompact) {
2024-07-08 11:56:03 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
2024-08-01 20:42:38 +00:00
children: [
2024-09-23 14:43:02 +00:00
_PostThumbnail(
rid: item.body['thumbnail'],
parentId: widget.item.id.toString(),
).paddingOnly(bottom: 8),
_PostHeaderWidget(
isCompact: widget.isCompact,
isFullDate: widget.isFullDate,
2024-09-23 14:43:02 +00:00
item: item,
).paddingSymmetric(horizontal: 12),
_PostHeaderDividerWidget(item: item).paddingSymmetric(horizontal: 12),
2024-10-11 16:41:03 +00:00
SizedContainer(
maxWidth: 640,
child: MarkdownTextContent(
parentId: 'p${item.id}',
content: item.body['content'],
isAutoWarp: item.type == 'story',
isSelectable: widget.isContentSelectable,
2024-10-11 16:41:03 +00:00
),
).paddingOnly(
left: 12,
right: 12,
bottom: hasAttachment ? 4 : 0,
2024-08-01 20:42:38 +00:00
),
if (widget.item.body?['content_truncated'] == true)
Opacity(
opacity: 0.8,
child: InkWell(child: Text('readMore'.tr)),
).paddingOnly(
left: 12,
top: 4,
),
2024-09-23 14:43:02 +00:00
LinkExpansion(content: item.body['content']).paddingOnly(
left: 8,
right: 8,
),
if (attachments.isNotEmpty)
Row(
children: [
Icon(
Icons.file_copy,
size: 15,
color: _unFocusColor,
).paddingOnly(right: 5),
Text(
'attachmentHint'.trParams(
{'count': attachments.length.toString()},
),
style: TextStyle(color: _unFocusColor),
)
],
2024-10-11 16:41:03 +00:00
).paddingOnly(left: 14, top: 4),
2024-08-01 20:42:38 +00:00
],
2024-09-23 14:43:02 +00:00
);
}
2024-10-12 17:31:59 +00:00
return GestureDetector(
child: Column(
2024-09-23 14:43:02 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
2024-08-01 20:42:38 +00:00
children: [
2024-09-23 14:43:02 +00:00
_PostThumbnail(
rid: item.body['thumbnail'],
parentId: widget.item.id.toString(),
).paddingOnly(bottom: 4),
2024-10-11 16:41:03 +00:00
Column(
2024-09-23 14:43:02 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
2024-08-01 20:42:38 +00:00
children: [
2024-10-11 16:41:03 +00:00
_PostHeaderWidget(
isCompact: widget.isCompact,
isFullDate: widget.isFullDate,
2024-10-11 16:41:03 +00:00
item: item,
2024-08-01 20:42:38 +00:00
),
2024-10-11 16:41:03 +00:00
_PostHeaderDividerWidget(item: item),
SizedContainer(
maxWidth: 640,
child: MarkdownTextContent(
parentId: 'p${item.id}-embed',
content: item.body['content'],
isAutoWarp: item.type == 'story',
isSelectable: widget.isContentSelectable,
2024-09-23 14:43:02 +00:00
),
2024-08-01 20:42:38 +00:00
),
if (widget.item.body?['content_truncated'] == true)
2024-10-11 16:41:03 +00:00
Opacity(
opacity: 0.8,
child: InkWell(child: Text('readMore'.tr)),
).paddingOnly(top: 4),
if (widget.item.replyTo != null && widget.isShowEmbed)
Container(
constraints: const BoxConstraints(maxWidth: 480),
padding: const EdgeInsets.only(top: 8),
child: _PostEmbedWidget(
isClickable: widget.isClickable,
isOverrideEmbedClickable: widget.isOverrideEmbedClickable,
item: widget.item.replyTo!,
username: widget.item.replyTo!.author.name,
hintText: 'postRepliedNotify',
icon: FontAwesomeIcons.reply,
id: widget.item.replyTo!.id.toString(),
),
),
if (widget.item.repostTo != null && widget.isShowEmbed)
Container(
constraints: const BoxConstraints(maxWidth: 480),
padding: const EdgeInsets.only(top: 8),
child: _PostEmbedWidget(
isClickable: widget.isClickable,
isOverrideEmbedClickable: widget.isOverrideEmbedClickable,
item: widget.item.repostTo!,
username: widget.item.repostTo!.author.name,
hintText: 'postRepostedNotify',
icon: FontAwesomeIcons.retweet,
id: widget.item.repostTo!.id.toString(),
),
),
_PostFooterWidget(item: item),
2024-10-12 17:31:59 +00:00
LinkExpansion(content: item.body['content']),
2024-08-01 20:42:38 +00:00
],
2024-10-12 17:31:59 +00:00
).paddingSymmetric(
horizontal: (widget.padding?.horizontal ?? 0) + 16,
),
if (hasAttachment) const Gap(8),
_PostAttachmentWidget(
item: item,
padding: widget.padding,
2024-08-01 20:42:38 +00:00
),
2024-10-11 16:41:03 +00:00
if (widget.showFeaturedReply)
_PostFeaturedReplyWidget(item: item).paddingSymmetric(
2024-10-12 17:31:59 +00:00
horizontal: (widget.padding?.horizontal ?? 0) + 12,
2024-10-11 16:41:03 +00:00
),
2024-09-23 14:43:02 +00:00
if (widget.isShowReply || widget.isReactable)
PostQuickAction(
isShowReply: widget.isShowReply,
isReactable: widget.isReactable,
item: widget.item,
onReact: (symbol, changes) {
setState(() {
item.metric!.reactionList[symbol] =
(item.metric!.reactionList[symbol] ?? 0) + changes;
});
},
2024-10-11 16:41:03 +00:00
onComment: () {
if (widget.onComment != null) {
widget.onComment!();
}
},
2024-09-23 14:43:02 +00:00
).paddingOnly(
2024-10-12 17:31:59 +00:00
top: 8,
left: (widget.padding?.left ?? 0) + 14,
right: (widget.padding?.right ?? 0) + 14,
2024-09-23 14:43:02 +00:00
)
2024-08-01 20:42:38 +00:00
],
2024-10-12 17:31:59 +00:00
).paddingOnly(
top: widget.padding?.top ?? 0,
bottom: widget.padding?.bottom ?? 0,
2024-08-01 20:42:38 +00:00
),
2024-10-12 17:31:59 +00:00
onTap: () {
if (widget.isClickable) {
AppRouter.instance.pushNamed(
'postDetail',
pathParameters: {'id': item.id.toString()},
);
}
},
);
}
2024-09-23 14:43:02 +00:00
}
2024-09-23 14:43:02 +00:00
class _PostFeaturedReplyWidget extends StatelessWidget {
final Post item;
2024-08-19 14:13:25 +00:00
2024-09-23 14:43:02 +00:00
const _PostFeaturedReplyWidget({required this.item});
2024-08-19 14:13:25 +00:00
2024-09-23 14:43:02 +00:00
@override
Widget build(BuildContext context) {
final unFocusColor =
Theme.of(context).colorScheme.onSurface.withOpacity(0.75);
if ((item.metric?.replyCount ?? 0) == 0) {
2024-09-16 15:35:44 +00:00
return const SizedBox.shrink();
}
2024-09-23 14:43:02 +00:00
2024-09-16 15:35:44 +00:00
return FutureBuilder(
2024-10-11 16:41:03 +00:00
future: Get.find<PostProvider>().listPostFeaturedReply(
item.id.toString(),
),
2024-09-16 15:35:44 +00:00
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const SizedBox.shrink();
}
2024-09-23 14:43:02 +00:00
2024-09-17 15:50:49 +00:00
return Container(
2024-10-12 17:31:59 +00:00
padding: EdgeInsets.only(top: 8),
2024-09-17 15:50:49 +00:00
constraints: const BoxConstraints(maxWidth: 480),
child: Card(
margin: EdgeInsets.zero,
child: Column(
children: snapshot.data!
.map(
2024-09-23 15:34:25 +00:00
(reply) => ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(8)),
child: OpenContainer(
closedBuilder: (_, openContainer) => Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2024-10-11 16:41:03 +00:00
AttachedCircleAvatar(
2024-09-23 15:34:25 +00:00
content: reply.author.avatar,
radius: 10,
),
const Gap(6),
Text(
reply.author.nick,
style:
const TextStyle(fontWeight: FontWeight.bold),
),
const Gap(6),
Text(
format(
reply.publishedAt?.toLocal() ?? DateTime.now(),
locale: 'en_short',
2024-09-16 15:35:44 +00:00
),
2024-09-23 15:34:25 +00:00
).paddingOnly(top: 0.5),
const Gap(8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MarkdownTextContent(
isAutoWarp: reply.type == 'story',
content: reply.body['content'],
parentId:
'p${item.id}-featured-reply${reply.id}',
),
if (reply.body['attachments'] is List &&
reply.body['attachments'].isNotEmpty)
Row(
children: [
Icon(
Icons.file_copy,
size: 15,
color: unFocusColor,
).paddingOnly(right: 5),
Text(
'attachmentHint'.trParams(
{
'count': reply
.body['attachments'].length
.toString(),
},
),
style: TextStyle(color: unFocusColor),
),
],
2024-09-23 14:43:02 +00:00
),
2024-09-23 15:34:25 +00:00
],
),
),
],
).paddingSymmetric(horizontal: 12, vertical: 8),
openBuilder: (_, __) => TitleShell(
title: 'postDetail'.tr,
child: PostDetailScreen(
id: reply.id.toString(),
post: reply,
2024-09-17 15:50:49 +00:00
),
2024-09-16 15:35:44 +00:00
),
2024-09-23 15:34:25 +00:00
closedElevation: 0,
openElevation: 0,
closedColor:
Theme.of(context).colorScheme.surfaceContainer,
openColor: Theme.of(context).colorScheme.surface,
),
),
2024-09-17 15:50:49 +00:00
)
.toList(),
),
2024-09-16 15:35:44 +00:00
),
2024-10-11 16:41:03 +00:00
).animate().fadeIn(
duration: 300.ms,
curve: Curves.easeIn,
);
2024-09-16 15:35:44 +00:00
},
);
}
2024-09-23 14:43:02 +00:00
}
2024-09-16 15:35:44 +00:00
2024-09-23 14:43:02 +00:00
class _PostAttachmentWidget extends StatelessWidget {
final Post item;
2024-10-12 17:31:59 +00:00
final EdgeInsets? padding;
2024-09-23 14:43:02 +00:00
2024-10-12 17:31:59 +00:00
const _PostAttachmentWidget({required this.item, required this.padding});
2024-05-18 10:17:16 +00:00
@override
Widget build(BuildContext context) {
2024-09-23 14:43:02 +00:00
final isLargeScreen = AppTheme.isLargeScreen(context);
final List<String> attachments = item.body['attachments'] is List
? List.from(item.body['attachments']?.whereType<String>())
2024-07-25 08:08:46 +00:00
: List.empty();
2024-05-19 12:30:50 +00:00
2024-10-11 16:41:03 +00:00
if (attachments.isEmpty) return const SizedBox.shrink();
2024-10-12 17:31:59 +00:00
if (attachments.length == 1 && !isLargeScreen) {
2024-09-23 14:43:02 +00:00
return AttachmentList(
parentId: item.id.toString(),
2024-10-10 14:52:05 +00:00
attachmentIds: item.preload == null ? attachments : null,
attachments: item.preload?.attachments,
2024-09-23 14:43:02 +00:00
autoload: false,
2024-10-11 16:41:03 +00:00
isFullWidth: true,
2024-10-12 17:31:59 +00:00
);
} else if (attachments.length == 1) {
return AttachmentList(
parentId: item.id.toString(),
attachmentIds: item.preload == null ? attachments : null,
attachments: item.preload?.attachments,
autoload: false,
isColumn: true,
).paddingSymmetric(horizontal: (padding?.horizontal ?? 0) + 14);
2024-10-11 16:41:03 +00:00
} else if (attachments.length > 1 &&
attachments.length % 3 == 0 &&
!isLargeScreen) {
2024-09-23 14:43:02 +00:00
return AttachmentList(
parentId: item.id.toString(),
2024-10-10 14:52:05 +00:00
attachmentIds: item.preload == null ? attachments : null,
attachments: item.preload?.attachments,
2024-09-23 14:43:02 +00:00
autoload: false,
2024-10-11 16:41:03 +00:00
isGrid: true,
2024-10-12 17:31:59 +00:00
).paddingSymmetric(horizontal: (padding?.horizontal ?? 0) + 14);
2024-09-23 14:43:02 +00:00
} else {
return AttachmentList(
parentId: item.id.toString(),
2024-10-10 14:52:05 +00:00
attachmentIds: item.preload == null ? attachments : null,
attachments: item.preload?.attachments,
2024-10-12 17:31:59 +00:00
padding: EdgeInsets.symmetric(
horizontal: (padding?.horizontal ?? 0) + 14,
),
2024-09-23 14:43:02 +00:00
autoload: false,
2024-10-12 17:31:59 +00:00
);
}
2024-09-23 14:43:02 +00:00
}
}
class _PostEmbedWidget extends StatelessWidget {
final bool isClickable;
final bool isOverrideEmbedClickable;
final Post item;
final String username;
final String hintText;
final IconData icon;
final String id;
const _PostEmbedWidget({
required this.isClickable,
required this.isOverrideEmbedClickable,
required this.item,
required this.username,
required this.hintText,
required this.icon,
required this.id,
});
@override
Widget build(BuildContext context) {
final unFocusColor =
Theme.of(context).colorScheme.onSurface.withOpacity(0.75);
2024-08-01 20:42:38 +00:00
return OpenContainer(
2024-09-23 14:43:02 +00:00
tappable: isClickable || isOverrideEmbedClickable,
2024-08-01 20:42:38 +00:00
closedBuilder: (_, openContainer) => Column(
children: [
Row(
children: [
2024-09-23 14:43:02 +00:00
FaIcon(
icon,
size: 16,
color: unFocusColor,
2024-08-01 20:42:38 +00:00
),
2024-10-11 16:41:03 +00:00
const Gap(6),
2024-08-01 20:42:38 +00:00
Expanded(
2024-09-23 14:43:02 +00:00
child: Text(
hintText.trParams(
{'username': '@$username'},
),
style: TextStyle(color: unFocusColor),
2024-10-11 16:41:03 +00:00
),
2024-05-18 10:17:16 +00:00
),
2024-08-01 20:42:38 +00:00
],
2024-10-11 16:41:03 +00:00
).paddingOnly(left: 2),
2024-09-23 14:43:02 +00:00
Card(
elevation: 1,
child: PostItem(
item: item,
isCompact: true,
attachmentParent: id,
).paddingSymmetric(vertical: 8),
2024-08-01 20:42:38 +00:00
),
],
),
openBuilder: (_, __) => TitleShell(
title: 'postDetail'.tr,
child: PostDetailScreen(
2024-09-23 14:43:02 +00:00
id: id,
2024-08-01 20:42:38 +00:00
post: item,
),
),
2024-08-01 21:10:10 +00:00
closedElevation: 0,
openElevation: 0,
closedColor: Colors.transparent,
2024-08-01 20:42:38 +00:00
openColor: Theme.of(context).colorScheme.surface,
2024-05-18 10:17:16 +00:00
);
}
}
2024-09-23 14:43:02 +00:00
class _PostHeaderDividerWidget extends StatelessWidget {
final Post item;
const _PostHeaderDividerWidget({
required this.item,
});
@override
Widget build(BuildContext context) {
if (item.body['description'] != null || item.body['title'] != null) {
2024-10-11 16:41:03 +00:00
return const Gap(8);
2024-09-23 14:43:02 +00:00
}
return const SizedBox.shrink();
}
}
class _PostFooterWidget extends StatelessWidget {
final Post item;
const _PostFooterWidget({required this.item});
@override
Widget build(BuildContext context) {
final unFocusColor =
Theme.of(context).colorScheme.onSurface.withOpacity(0.75);
List<String> labels = List.empty(growable: true);
if (item.editedAt != null) {
labels.add('postEdited'.trParams({
'date': DateFormat('yy/M/d HH:mm').format(item.editedAt!.toLocal()),
}));
}
if (item.realm != null) {
labels.add('postInRealm'.trParams({
'realm': item.realm!.alias,
}));
}
List<Widget> widgets = List.empty(growable: true);
if (item.tags?.isNotEmpty ?? false) {
widgets.add(PostTagsList(tags: item.tags!));
}
if (labels.isNotEmpty) {
widgets.add(Text(
labels.join(' · '),
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 12,
color: unFocusColor,
),
));
}
if (item.pinnedAt != null) {
widgets.add(Text(
'postPinned'.tr,
style: TextStyle(fontSize: 12, color: unFocusColor),
));
}
if (widgets.isEmpty) {
return const SizedBox.shrink();
} else {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: widgets,
2024-10-12 17:31:59 +00:00
).paddingSymmetric(vertical: 4);
2024-09-23 14:43:02 +00:00
}
}
}
class _PostHeaderWidget extends StatelessWidget {
final bool isCompact;
final bool isFullDate;
2024-09-23 14:43:02 +00:00
final Post item;
const _PostHeaderWidget({
required this.isCompact,
required this.isFullDate,
2024-09-23 14:43:02 +00:00
required this.item,
});
@override
Widget build(BuildContext context) {
2024-10-11 16:41:03 +00:00
return Column(
2024-09-23 14:43:02 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2024-10-11 16:41:03 +00:00
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AccountAvatar(
content: item.author.avatar,
username: item.author.name,
radius: isCompact ? 10 : null,
),
Gap(isCompact ? 6 : 8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2024-09-23 14:43:02 +00:00
children: [
2024-10-11 16:41:03 +00:00
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
item.author.nick,
style: const TextStyle(fontWeight: FontWeight.bold),
),
if (isCompact) const Gap(4),
if (isCompact)
RelativeDate(
item.publishedAt?.toLocal() ?? DateTime.now(),
isFull: isFullDate,
2024-10-11 16:41:03 +00:00
).paddingOnly(top: 1),
],
2024-09-23 14:43:02 +00:00
),
2024-10-11 16:41:03 +00:00
if (!isCompact)
RelativeDate(item.publishedAt?.toLocal() ?? DateTime.now()),
2024-09-23 14:43:02 +00:00
],
),
2024-10-11 16:41:03 +00:00
),
if (item.type == 'article')
Badge(
label: Text('article'.tr),
).paddingOnly(top: 3),
],
2024-09-23 14:43:02 +00:00
),
2024-10-11 16:41:03 +00:00
const Gap(8),
if (item.body['title'] != null)
Text(
item.body['title'],
style: Theme.of(context).textTheme.titleMedium,
),
if (item.body['description'] != null)
Text(
item.body['description'],
style: Theme.of(context).textTheme.titleSmall,
),
2024-09-23 14:43:02 +00:00
],
);
}
}
class _PostThumbnail extends StatelessWidget {
final String parentId;
final String? rid;
const _PostThumbnail({required this.parentId, required this.rid});
@override
Widget build(BuildContext context) {
if (rid?.isEmpty ?? true) return const SizedBox.shrink();
final border = BorderSide(
color: Theme.of(context).dividerColor,
width: 0.3,
);
return Container(
decoration: BoxDecoration(border: Border(top: border, bottom: border)),
child: AspectRatio(
aspectRatio: 16 / 9,
child: AttachmentSelfContainedEntry(
rid: rid!,
parentId: 'p$parentId-thumbnail',
),
),
);
}
}