Surface/lib/widgets/post/post_item.dart

228 lines
7.0 KiB
Dart
Raw Normal View History

2024-11-10 10:37:34 +00:00
import 'package:easy_localization/easy_localization.dart';
2024-11-08 17:11:56 +00:00
import 'package:flutter/material.dart';
2024-11-10 10:37:34 +00:00
import 'package:go_router/go_router.dart';
2024-11-09 03:16:14 +00:00
import 'package:material_symbols_icons/symbols.dart';
import 'package:provider/provider.dart';
2024-11-09 03:16:14 +00:00
import 'package:relative_time/relative_time.dart';
import 'package:styled_widget/styled_widget.dart';
import 'package:surface/providers/userinfo.dart';
2024-11-08 17:11:56 +00:00
import 'package:surface/types/post.dart';
2024-11-09 03:16:14 +00:00
import 'package:surface/widgets/account/account_image.dart';
2024-11-09 04:04:03 +00:00
import 'package:surface/widgets/attachment/attachment_list.dart';
2024-11-09 03:16:14 +00:00
import 'package:surface/widgets/markdown_content.dart';
import 'package:gap/gap.dart';
2024-11-11 12:06:00 +00:00
import 'package:surface/widgets/post/post_comment_list.dart';
2024-11-08 17:11:56 +00:00
class PostItem extends StatelessWidget {
final SnPost data;
2024-11-11 12:06:00 +00:00
final bool showComments;
const PostItem({
super.key,
required this.data,
this.showComments = true,
});
2024-11-08 17:11:56 +00:00
@override
Widget build(BuildContext context) {
return Column(
2024-11-09 03:16:14 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_PostContentHeader(data: data).padding(horizontal: 12, vertical: 8),
2024-11-09 03:16:14 +00:00
_PostContentBody(data: data.body).padding(horizontal: 16, bottom: 6),
2024-11-09 04:04:03 +00:00
if (data.preload?.attachments?.isNotEmpty ?? true)
2024-11-11 12:06:00 +00:00
AttachmentList(
data: data.preload!.attachments!,
bordered: true,
),
_PostBottomAction(data: data, showComments: showComments)
.padding(left: 12, right: 18),
],
);
}
}
class _PostBottomAction extends StatelessWidget {
final SnPost data;
2024-11-11 12:06:00 +00:00
final bool showComments;
const _PostBottomAction({required this.data, required this.showComments});
@override
Widget build(BuildContext context) {
final iconColor = Theme.of(context).colorScheme.onSurface.withAlpha(
(255 * 0.8).round(),
);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
InkWell(
child: Row(
children: [
Icon(Symbols.add_reaction, size: 20, color: iconColor),
const Gap(8),
Text('postReact').tr(),
],
2024-11-11 12:06:00 +00:00
).padding(horizontal: 8, vertical: 8),
onTap: () {},
),
2024-11-11 12:06:00 +00:00
if (showComments)
InkWell(
child: Row(
children: [
Icon(Symbols.comment, size: 20, color: iconColor),
const Gap(8),
Text('postComments').plural(data.metric.replyCount),
],
).padding(horizontal: 8, vertical: 8),
onTap: () {
showModalBottomSheet(
context: context,
useRootNavigator: true,
builder: (context) => PostCommentListPopup(
postId: data.id,
commentCount: data.metric.replyCount,
),
);
},
),
].expand((ele) => [ele, const Gap(8)]).toList()
..removeLast(),
),
InkWell(
2024-11-11 12:06:00 +00:00
child: Icon(
Symbols.share,
size: 20,
color: iconColor,
).padding(horizontal: 8, vertical: 8),
onTap: () {},
),
2024-11-09 03:16:14 +00:00
],
2024-11-08 17:11:56 +00:00
);
}
}
2024-11-09 03:16:14 +00:00
class _PostContentHeader extends StatelessWidget {
final SnPost data;
const _PostContentHeader({required this.data});
@override
Widget build(BuildContext context) {
final ua = context.read<UserProvider>();
final isAuthor = ua.isAuthorized && data.publisher.accountId == ua.user!.id;
2024-11-09 03:16:14 +00:00
return Row(
children: [
AccountImage(content: data.publisher.avatar),
const Gap(12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(data.publisher.nick).bold(),
Row(
children: [
Text('@${data.publisher.name}').fontSize(13),
const Gap(4),
Text(RelativeTime(context).format(
data.publishedAt ?? data.createdAt,
)).fontSize(13),
2024-11-09 03:16:14 +00:00
],
).opacity(0.8),
],
),
),
2024-11-10 10:37:34 +00:00
PopupMenuButton(
icon: const Icon(Symbols.more_horiz),
style: const ButtonStyle(
visualDensity: VisualDensity(horizontal: -4, vertical: -4),
2024-11-09 03:16:14 +00:00
),
2024-11-10 10:37:34 +00:00
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
if (isAuthor)
PopupMenuItem(
child: Row(
children: [
const Icon(Symbols.edit),
const Gap(16),
Text('edit').tr(),
],
),
onTap: () {
GoRouter.of(context).pushNamed(
'postEditor',
pathParameters: {'mode': data.typePlural},
queryParameters: {'editing': data.id.toString()},
);
},
),
if (isAuthor)
PopupMenuItem(
child: Row(
children: [
const Icon(Symbols.delete),
const Gap(16),
Text('delete').tr(),
],
),
),
if (isAuthor) const PopupMenuDivider(),
2024-11-10 10:37:34 +00:00
PopupMenuItem(
child: Row(
children: [
const Icon(Symbols.reply),
2024-11-10 10:37:34 +00:00
const Gap(16),
Text('reply').tr(),
2024-11-10 10:37:34 +00:00
],
),
onTap: () {
GoRouter.of(context).pushNamed(
'postEditor',
pathParameters: {'mode': data.typePlural},
queryParameters: {'replying': data.id.toString()},
2024-11-10 10:37:34 +00:00
);
},
),
PopupMenuItem(
child: Row(
children: [
const Icon(Symbols.forward),
2024-11-10 10:37:34 +00:00
const Gap(16),
Text('repost').tr(),
2024-11-10 10:37:34 +00:00
],
),
onTap: () {
GoRouter.of(context).pushNamed(
'postEditor',
pathParameters: {'mode': data.typePlural},
queryParameters: {'reposting': data.id.toString()},
);
},
2024-11-10 10:37:34 +00:00
),
const PopupMenuDivider(),
PopupMenuItem(
child: Row(
children: [
const Icon(Symbols.flag),
const Gap(16),
Text('report').tr(),
],
),
),
],
2024-11-09 03:16:14 +00:00
),
],
);
2024-11-09 03:16:14 +00:00
}
}
class _PostContentBody extends StatelessWidget {
final dynamic data;
const _PostContentBody({this.data});
2024-11-08 17:11:56 +00:00
@override
Widget build(BuildContext context) {
2024-11-09 03:16:14 +00:00
if (data['content'] == null) return const SizedBox.shrink();
return MarkdownTextContent(content: data['content']);
2024-11-08 17:11:56 +00:00
}
}