Solian/lib/widgets/posts/post_item.dart

194 lines
5.6 KiB
Dart
Raw Normal View History

2024-05-18 10:17:16 +00:00
import 'package:flutter/cupertino.dart';
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';
import 'package:solian/models/post.dart';
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/posts/post_quick_action.dart';
2024-05-18 10:17:16 +00:00
import 'package:timeago/timeago.dart' show format;
class PostItem extends StatefulWidget {
final Post item;
final bool isCompact;
final bool isReactable;
2024-05-18 10:17:16 +00:00
const PostItem({
super.key,
required this.item,
this.isCompact = false,
this.isReactable = true,
});
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();
}
Widget buildReply(BuildContext context) {
return Column(
children: [
Row(
children: [
2024-05-23 12:00:26 +00:00
FaIcon(
FontAwesomeIcons.reply,
size: 16,
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.75),
),
Text(
'postRepliedNotify'.trParams(
{'username': '@${widget.item.author.name}'},
),
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,
).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,
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.75),
),
Text(
'postRepostedNotify'.trParams(
{'username': '@${widget.item.author.name}'},
),
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,
).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;
if (widget.isCompact) {
return Column(
children: [
Row(
children: [
AccountAvatar(content: item.author.avatar.toString(), radius: 10),
Text(
item.author.nick,
style: const TextStyle(fontWeight: FontWeight.bold),
).paddingOnly(left: 6),
Text(format(item.createdAt, locale: 'en_short'))
.paddingOnly(left: 4),
],
).paddingSymmetric(horizontal: 12),
Markdown(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
data: item.content,
padding: const EdgeInsets.all(0),
).paddingOnly(
left: 16,
right: 12,
top: 2,
bottom: hasAttachment ? 4 : 0,
),
AttachmentList(attachmentsId: item.attachments ?? List.empty()),
],
);
}
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: [
AccountAvatar(content: item.author.avatar.toString()),
2024-05-18 14:23:36 +00:00
Expanded(
child: Column(
2024-05-18 10:17:16 +00:00
children: [
2024-05-18 14:23:36 +00:00
Row(
children: [
Text(
2024-05-19 12:30:50 +00:00
item.author.nick,
2024-05-18 14:23:36 +00:00
style: const TextStyle(fontWeight: FontWeight.bold),
2024-05-18 16:56:32 +00:00
).paddingOnly(left: 12),
Text(format(item.createdAt, locale: 'en_short'))
.paddingOnly(left: 4),
2024-05-18 14:23:36 +00:00
],
),
Markdown(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
2024-05-19 12:30:50 +00:00
data: item.content,
2024-05-18 14:23:36 +00:00
padding: const EdgeInsets.all(0),
2024-05-18 16:56:32 +00:00
).paddingOnly(left: 12, right: 8),
2024-05-23 12:00:26 +00:00
if (widget.item.replyTo != null)
buildReply(context).paddingOnly(top: 4),
if (widget.item.repostTo != null)
buildRepost(context).paddingOnly(top: 4),
2024-05-18 10:17:16 +00:00
],
),
2024-05-18 14:23:36 +00:00
)
],
).paddingOnly(
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-19 12:30:50 +00:00
AttachmentList(attachmentsId: item.attachments ?? List.empty()),
PostQuickAction(
isReactable: widget.isReactable,
2024-05-19 12:30:50 +00:00
item: widget.item,
onReact: (symbol, changes) {
setState(() {
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
],
);
}
}