2024-05-18 10:17:16 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_markdown/flutter_markdown.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-05-21 16:05:03 +00:00
|
|
|
import 'package:solian/widgets/posts/post_quick_action.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:timeago/timeago.dart' show format;
|
2024-05-26 05:39:21 +00:00
|
|
|
import 'package:url_launcher/url_launcher_string.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
|
|
|
|
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-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-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;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
item = widget.item;
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-06-22 17:52:05 +00:00
|
|
|
Widget buildDate() {
|
|
|
|
if (widget.isFullDate) {
|
2024-06-23 03:29:34 +00:00
|
|
|
return Text(DateFormat('y/M/d H:m').format(item.createdAt.toLocal()));
|
2024-06-22 17:52:05 +00:00
|
|
|
} else {
|
2024-06-23 03:29:34 +00:00
|
|
|
return Text(format(item.createdAt.toLocal(), locale: 'en_short'));
|
2024-06-22 17:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget buildHeader() {
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
item.author.nick,
|
|
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
).paddingOnly(left: 12),
|
|
|
|
buildDate().paddingOnly(left: 4),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget buildBody() {
|
|
|
|
return Markdown(
|
|
|
|
shrinkWrap: true,
|
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
data: item.content,
|
|
|
|
padding: const EdgeInsets.all(0),
|
|
|
|
onTapLink: (text, href, title) async {
|
|
|
|
if (href == null) return;
|
|
|
|
await launchUrlString(
|
|
|
|
href,
|
|
|
|
mode: LaunchMode.externalApplication,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget buildFooter() {
|
|
|
|
List<String> labels = List.empty(growable: true);
|
|
|
|
if (widget.item.createdAt != widget.item.updatedAt) {
|
|
|
|
labels.add('postEdited'.trParams({
|
2024-06-23 03:29:34 +00:00
|
|
|
'date': DateFormat('yy/M/d H:m').format(item.updatedAt.toLocal()),
|
2024-06-22 17:52:05 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (widget.item.realm != null) {
|
|
|
|
labels.add('postInRealm'.trParams({
|
|
|
|
'realm': '#${widget.item.realm!.alias}',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (labels.isNotEmpty) {
|
|
|
|
return Text(
|
|
|
|
labels.join(" · "),
|
|
|
|
textAlign: TextAlign.left,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 12,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.75),
|
|
|
|
),
|
|
|
|
).paddingOnly(top: 8);
|
|
|
|
} else {
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 12:56:10 +00:00
|
|
|
Widget buildReply(BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
2024-05-23 12:00:26 +00:00
|
|
|
FaIcon(
|
|
|
|
FontAwesomeIcons.reply,
|
|
|
|
size: 16,
|
2024-05-22 12:56:10 +00:00
|
|
|
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.75),
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
'postRepliedNotify'.trParams(
|
2024-05-25 16:11:00 +00:00
|
|
|
{'username': '@${widget.item.replyTo!.author.name}'},
|
2024-05-22 12:56:10 +00:00
|
|
|
),
|
|
|
|
style: TextStyle(
|
|
|
|
color:
|
|
|
|
Theme.of(context).colorScheme.onSurface.withOpacity(0.75),
|
|
|
|
),
|
|
|
|
).paddingOnly(left: 6),
|
|
|
|
],
|
|
|
|
).paddingOnly(left: 12),
|
|
|
|
Card(
|
|
|
|
elevation: 1,
|
|
|
|
child: PostItem(
|
|
|
|
item: widget.item.replyTo!,
|
|
|
|
isCompact: true,
|
2024-05-25 16:11:00 +00:00
|
|
|
overrideAttachmentParent: widget.item.alias,
|
2024-05-22 12:56:10 +00:00
|
|
|
).paddingSymmetric(vertical: 8),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget buildRepost(BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
2024-05-23 12:00:26 +00:00
|
|
|
FaIcon(
|
|
|
|
FontAwesomeIcons.retweet,
|
|
|
|
size: 16,
|
2024-05-22 12:56:10 +00:00
|
|
|
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.75),
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
'postRepostedNotify'.trParams(
|
2024-05-25 16:11:00 +00:00
|
|
|
{'username': '@${widget.item.repostTo!.author.name}'},
|
2024-05-22 12:56:10 +00:00
|
|
|
),
|
|
|
|
style: TextStyle(
|
|
|
|
color:
|
|
|
|
Theme.of(context).colorScheme.onSurface.withOpacity(0.75),
|
|
|
|
),
|
|
|
|
).paddingOnly(left: 6),
|
|
|
|
],
|
|
|
|
).paddingOnly(left: 12),
|
|
|
|
Card(
|
|
|
|
elevation: 1,
|
|
|
|
child: PostItem(
|
|
|
|
item: widget.item.repostTo!,
|
|
|
|
isCompact: true,
|
2024-05-25 16:11:00 +00:00
|
|
|
overrideAttachmentParent: widget.item.alias,
|
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-05-19 12:30:50 +00:00
|
|
|
final hasAttachment = item.attachments?.isNotEmpty ?? false;
|
|
|
|
|
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-06-22 17:52:05 +00:00
|
|
|
buildHeader().paddingSymmetric(horizontal: 12),
|
|
|
|
buildBody().paddingOnly(
|
2024-05-22 12:56:10 +00:00
|
|
|
left: 16,
|
|
|
|
right: 12,
|
|
|
|
top: 2,
|
|
|
|
bottom: hasAttachment ? 4 : 0,
|
|
|
|
),
|
2024-06-22 17:52:05 +00:00
|
|
|
buildFooter().paddingOnly(left: 16),
|
2024-05-25 16:11:00 +00:00
|
|
|
AttachmentList(
|
|
|
|
parentId: widget.overrideAttachmentParent ?? widget.item.alias,
|
|
|
|
attachmentsId: item.attachments ?? List.empty(),
|
2024-06-22 17:52:05 +00:00
|
|
|
divided: true,
|
2024-05-25 16:11:00 +00:00
|
|
|
),
|
2024-05-22 12:56:10 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-18 14:23:36 +00:00
|
|
|
return Column(
|
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-06-22 17:52:05 +00:00
|
|
|
buildHeader(),
|
|
|
|
buildBody().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(
|
|
|
|
child: buildReply(context).paddingOnly(top: 4),
|
|
|
|
onTap: () {
|
|
|
|
if (!widget.isClickable) return;
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'postDetail',
|
|
|
|
pathParameters: {
|
|
|
|
'alias': widget.item.replyTo!.alias,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-05-25 09:21:27 +00:00
|
|
|
if (widget.item.repostTo != null && widget.isShowEmbed)
|
2024-05-25 05:19:16 +00:00
|
|
|
GestureDetector(
|
|
|
|
child: buildRepost(context).paddingOnly(top: 4),
|
|
|
|
onTap: () {
|
|
|
|
if (!widget.isClickable) return;
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'postDetail',
|
|
|
|
pathParameters: {
|
|
|
|
'alias': widget.item.repostTo!.alias,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-06-22 17:52:05 +00:00
|
|
|
buildFooter().paddingOnly(left: 12),
|
2024-05-18 10:17:16 +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-05-25 16:11:00 +00:00
|
|
|
AttachmentList(
|
|
|
|
parentId: widget.item.alias,
|
|
|
|
attachmentsId: item.attachments ?? List.empty(),
|
2024-06-22 17:52:05 +00:00
|
|
|
divided: true,
|
2024-05-25 16:11:00 +00:00
|
|
|
),
|
2024-05-19 12:30:50 +00:00
|
|
|
PostQuickAction(
|
2024-05-25 05:19:16 +00:00
|
|
|
isShowReply: widget.isShowReply,
|
2024-05-21 16:05:03 +00:00
|
|
|
isReactable: widget.isReactable,
|
2024-05-19 12:30:50 +00:00
|
|
|
item: widget.item,
|
|
|
|
onReact: (symbol, changes) {
|
|
|
|
setState(() {
|
2024-05-21 16:05:03 +00:00
|
|
|
item.reactionList[symbol] =
|
|
|
|
(item.reactionList[symbol] ?? 0) + changes;
|
2024-05-19 12:30:50 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
).paddingOnly(
|
|
|
|
top: hasAttachment ? 10 : 6,
|
|
|
|
left: hasAttachment ? 16 : 60,
|
|
|
|
right: 16,
|
|
|
|
bottom: 10,
|
|
|
|
),
|
2024-05-18 10:17:16 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|