2024-04-13 11:47:31 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-04-16 12:36:47 +00:00
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
2024-04-13 11:47:31 +00:00
|
|
|
import 'package:solian/models/post.dart';
|
2024-04-26 12:49:21 +00:00
|
|
|
import 'package:solian/widgets/account/avatar.dart';
|
2024-04-16 12:36:47 +00:00
|
|
|
import 'package:solian/widgets/posts/comment_list.dart';
|
2024-04-13 11:47:31 +00:00
|
|
|
import 'package:solian/widgets/posts/content/article.dart';
|
|
|
|
import 'package:solian/widgets/posts/content/attachment.dart';
|
|
|
|
import 'package:solian/widgets/posts/content/moment.dart';
|
2024-04-14 10:38:44 +00:00
|
|
|
import 'package:solian/widgets/posts/item_action.dart';
|
2024-04-15 15:08:32 +00:00
|
|
|
import 'package:solian/widgets/posts/reaction_list.dart';
|
2024-04-17 15:00:53 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-04-13 11:47:31 +00:00
|
|
|
import 'package:timeago/timeago.dart' as timeago;
|
|
|
|
|
|
|
|
class PostItem extends StatefulWidget {
|
|
|
|
final Post item;
|
2024-05-01 16:49:38 +00:00
|
|
|
final bool brief;
|
|
|
|
final bool ripple;
|
2024-04-14 10:38:44 +00:00
|
|
|
final Function? onUpdate;
|
2024-04-15 15:08:32 +00:00
|
|
|
final Function? onDelete;
|
2024-04-18 15:39:48 +00:00
|
|
|
final Function? onTap;
|
2024-04-13 11:47:31 +00:00
|
|
|
|
2024-04-15 15:08:32 +00:00
|
|
|
const PostItem({
|
|
|
|
super.key,
|
|
|
|
required this.item,
|
2024-05-01 16:49:38 +00:00
|
|
|
this.brief = true,
|
|
|
|
this.ripple = true,
|
2024-04-15 15:08:32 +00:00
|
|
|
this.onUpdate,
|
|
|
|
this.onDelete,
|
2024-04-18 15:39:48 +00:00
|
|
|
this.onTap,
|
2024-04-15 15:08:32 +00:00
|
|
|
});
|
2024-04-13 11:47:31 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<PostItem> createState() => _PostItemState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PostItemState extends State<PostItem> {
|
|
|
|
Map<String, dynamic>? reactionList;
|
|
|
|
|
2024-04-18 15:39:48 +00:00
|
|
|
void viewActions() {
|
2024-04-14 10:38:44 +00:00
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => PostItemAction(
|
|
|
|
item: widget.item,
|
|
|
|
onUpdate: widget.onUpdate,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-18 15:39:48 +00:00
|
|
|
void viewComments() {
|
2024-05-01 09:37:34 +00:00
|
|
|
final PagingController<int, Post> commentPaging =
|
|
|
|
PagingController(firstPageKey: 0);
|
2024-04-16 12:36:47 +00:00
|
|
|
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
CommentListHeader(
|
|
|
|
related: widget.item,
|
|
|
|
paging: commentPaging,
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
CommentList(
|
|
|
|
related: widget.item,
|
|
|
|
dataset: '${widget.item.modelType}s',
|
|
|
|
paging: commentPaging,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-13 11:47:31 +00:00
|
|
|
Widget renderContent() {
|
|
|
|
switch (widget.item.modelType) {
|
2024-04-13 17:45:27 +00:00
|
|
|
case 'article':
|
2024-05-01 16:49:38 +00:00
|
|
|
return ArticleContent(item: widget.item, brief: widget.brief);
|
2024-04-13 11:47:31 +00:00
|
|
|
default:
|
2024-05-01 16:49:38 +00:00
|
|
|
return MomentContent(item: widget.item, brief: widget.brief);
|
2024-04-13 11:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget renderAttachments() {
|
2024-04-13 17:45:27 +00:00
|
|
|
if (widget.item.modelType == 'article') return Container();
|
|
|
|
|
2024-05-01 09:37:34 +00:00
|
|
|
if (widget.item.attachments != null &&
|
|
|
|
widget.item.attachments!.isNotEmpty) {
|
2024-04-13 17:45:27 +00:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 8),
|
2024-05-01 09:37:34 +00:00
|
|
|
child: AttachmentList(
|
|
|
|
items: widget.item.attachments!, provider: 'interactive'),
|
2024-04-13 17:45:27 +00:00
|
|
|
);
|
2024-04-13 11:47:31 +00:00
|
|
|
} else {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 15:08:32 +00:00
|
|
|
Widget renderReactions() {
|
2024-04-15 15:40:36 +00:00
|
|
|
return Container(
|
|
|
|
height: 48,
|
|
|
|
padding: const EdgeInsets.only(top: 8, left: 4, right: 4),
|
2024-04-16 12:36:47 +00:00
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
ActionChip(
|
|
|
|
avatar: const Icon(Icons.comment),
|
|
|
|
label: Text(widget.item.commentCount.toString()),
|
2024-04-17 15:00:53 +00:00
|
|
|
tooltip: AppLocalizations.of(context)!.comment,
|
2024-04-18 15:39:48 +00:00
|
|
|
onPressed: () => viewComments(),
|
2024-04-16 12:36:47 +00:00
|
|
|
),
|
|
|
|
const VerticalDivider(thickness: 0.3, indent: 8, endIndent: 8),
|
|
|
|
Expanded(
|
|
|
|
child: ReactionList(
|
|
|
|
item: widget.item,
|
|
|
|
reactionList: reactionList,
|
|
|
|
onReact: (symbol, changes) {
|
|
|
|
setState(() {
|
|
|
|
if (!reactionList!.containsKey(symbol)) {
|
|
|
|
reactionList![symbol] = 0;
|
|
|
|
}
|
|
|
|
reactionList![symbol] += changes;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2024-04-15 15:40:36 +00:00
|
|
|
),
|
|
|
|
);
|
2024-04-15 15:08:32 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 09:37:34 +00:00
|
|
|
String getAuthorDescribe() => widget.item.author.description.isNotEmpty
|
|
|
|
? widget.item.author.description
|
|
|
|
: 'No description yet.';
|
2024-04-13 17:45:27 +00:00
|
|
|
|
2024-04-15 15:08:32 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2024-04-28 14:02:25 +00:00
|
|
|
reactionList = widget.item.reactionList ?? {};
|
2024-04-15 15:08:32 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-04-13 11:47:31 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-13 17:45:27 +00:00
|
|
|
final headingParts = [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
widget.item.author.nick,
|
|
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 4),
|
|
|
|
Text(timeago.format(widget.item.createdAt))
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2024-04-15 15:08:32 +00:00
|
|
|
Widget content;
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
if (widget.brief) {
|
2024-04-15 15:08:32 +00:00
|
|
|
content = Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2024-04-26 12:49:21 +00:00
|
|
|
AccountAvatar(
|
|
|
|
source: widget.item.author.avatar,
|
|
|
|
direct: true,
|
2024-04-15 15:08:32 +00:00
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
...headingParts,
|
|
|
|
Padding(
|
2024-05-01 09:37:34 +00:00
|
|
|
padding:
|
|
|
|
const EdgeInsets.only(left: 12, right: 12, top: 4),
|
2024-04-15 15:08:32 +00:00
|
|
|
child: renderContent(),
|
|
|
|
),
|
|
|
|
renderAttachments(),
|
|
|
|
renderReactions(),
|
|
|
|
],
|
2024-04-14 10:38:44 +00:00
|
|
|
),
|
2024-04-15 15:08:32 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
2024-04-13 17:45:27 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
2024-04-15 15:08:32 +00:00
|
|
|
content = Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
2024-05-01 16:49:38 +00:00
|
|
|
padding: const EdgeInsets.only(left: 20, right: 20, top: 16),
|
2024-04-15 15:08:32 +00:00
|
|
|
child: Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2024-04-26 12:49:21 +00:00
|
|
|
AccountAvatar(
|
|
|
|
source: widget.item.author.avatar,
|
|
|
|
direct: true,
|
2024-04-15 15:08:32 +00:00
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
...headingParts,
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
|
|
child: Text(
|
|
|
|
getAuthorDescribe(),
|
|
|
|
maxLines: 1,
|
2024-04-13 17:45:27 +00:00
|
|
|
),
|
2024-04-15 15:08:32 +00:00
|
|
|
),
|
|
|
|
],
|
2024-04-13 17:45:27 +00:00
|
|
|
),
|
2024-04-15 15:08:32 +00:00
|
|
|
),
|
|
|
|
],
|
2024-04-14 10:38:44 +00:00
|
|
|
),
|
2024-04-15 15:08:32 +00:00
|
|
|
),
|
|
|
|
const Padding(
|
|
|
|
padding: EdgeInsets.only(top: 6),
|
|
|
|
child: Divider(thickness: 0.3),
|
|
|
|
),
|
|
|
|
Padding(
|
2024-05-01 16:49:38 +00:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
|
2024-04-15 15:08:32 +00:00
|
|
|
child: renderContent(),
|
|
|
|
),
|
|
|
|
Padding(
|
2024-05-01 16:49:38 +00:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
2024-04-15 15:08:32 +00:00
|
|
|
child: renderAttachments(),
|
2024-04-15 15:47:44 +00:00
|
|
|
),
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
child: Padding(
|
2024-05-01 16:49:38 +00:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2),
|
2024-04-15 15:47:44 +00:00
|
|
|
child: renderReactions(),
|
|
|
|
),
|
|
|
|
),
|
2024-04-15 15:08:32 +00:00
|
|
|
],
|
2024-04-13 17:45:27 +00:00
|
|
|
);
|
|
|
|
}
|
2024-04-15 15:08:32 +00:00
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
if (widget.ripple) {
|
2024-04-18 15:39:48 +00:00
|
|
|
return InkWell(
|
|
|
|
child: content,
|
|
|
|
onTap: () {
|
|
|
|
if (widget.onTap != null) widget.onTap!();
|
|
|
|
},
|
|
|
|
onLongPress: () => viewActions(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return GestureDetector(
|
|
|
|
child: content,
|
|
|
|
onTap: () {
|
|
|
|
if (widget.onTap != null) widget.onTap!();
|
|
|
|
},
|
|
|
|
onLongPress: () => viewActions(),
|
|
|
|
);
|
|
|
|
}
|
2024-04-13 11:47:31 +00:00
|
|
|
}
|
|
|
|
}
|