Surface/lib/widgets/post/post_comment_list.dart

164 lines
4.7 KiB
Dart
Raw Permalink Normal View History

2024-11-11 12:06:00 +00:00
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:provider/provider.dart';
import 'package:styled_widget/styled_widget.dart';
2024-11-25 16:00:09 +00:00
import 'package:surface/providers/post.dart';
2024-11-14 14:49:17 +00:00
import 'package:surface/providers/userinfo.dart';
2024-11-11 12:06:00 +00:00
import 'package:surface/types/post.dart';
import 'package:surface/widgets/post/post_item.dart';
2024-11-11 14:43:09 +00:00
import 'package:surface/widgets/post/post_mini_editor.dart';
2024-11-11 12:06:00 +00:00
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
class PostCommentSliverList extends StatefulWidget {
final int parentPostId;
2024-11-14 14:49:17 +00:00
final double? maxWidth;
const PostCommentSliverList({
super.key,
required this.parentPostId,
this.maxWidth,
});
2024-11-11 12:06:00 +00:00
@override
2024-11-11 14:43:09 +00:00
State<PostCommentSliverList> createState() => PostCommentSliverListState();
2024-11-11 12:06:00 +00:00
}
2024-11-11 14:43:09 +00:00
class PostCommentSliverListState extends State<PostCommentSliverList> {
2024-11-11 12:06:00 +00:00
bool _isBusy = true;
final List<SnPost> _posts = List.empty(growable: true);
int? _postCount;
Future<void> _fetchPosts() async {
if (_postCount != null && _posts.length >= _postCount!) return;
setState(() => _isBusy = true);
2024-11-25 16:00:09 +00:00
final pt = context.read<SnPostContentProvider>();
final result = await pt.listPostReplies(widget.parentPostId);
final List<SnPost> out = result.$1;
2024-11-11 12:06:00 +00:00
if (!mounted) return;
2024-11-25 16:00:09 +00:00
_postCount = result.$2;
2024-11-11 12:06:00 +00:00
_posts.addAll(out);
if (mounted) setState(() => _isBusy = false);
}
2024-11-11 14:43:09 +00:00
Future<void> refresh() async {
_posts.clear();
_fetchPosts();
}
2024-11-11 12:06:00 +00:00
@override
void initState() {
super.initState();
_fetchPosts();
}
@override
Widget build(BuildContext context) {
return SliverInfiniteList(
itemCount: _posts.length,
isLoading: _isBusy,
hasReachedMax: _postCount != null && _posts.length >= _postCount!,
onFetchData: _fetchPosts,
itemBuilder: (context, idx) {
return GestureDetector(
child: PostItem(
data: _posts[idx],
maxWidth: widget.maxWidth,
2024-11-23 10:04:30 +00:00
onChanged: (data) {
setState(() => _posts[idx] = data);
},
2024-11-28 14:04:38 +00:00
onDeleted: () {
_posts.clear();
_fetchPosts();
},
),
2024-11-11 12:06:00 +00:00
onTap: () {
GoRouter.of(context).pushNamed(
'postDetail',
pathParameters: {'slug': _posts[idx].id.toString()},
extra: _posts[idx],
);
},
);
},
separatorBuilder: (context, index) => const Divider(height: 1),
);
}
}
2024-11-11 14:43:09 +00:00
class PostCommentListPopup extends StatefulWidget {
2024-11-11 12:06:00 +00:00
final int postId;
final int commentCount;
const PostCommentListPopup({
super.key,
required this.postId,
this.commentCount = 0,
});
2024-11-11 14:43:09 +00:00
@override
State<PostCommentListPopup> createState() => _PostCommentListPopupState();
}
class _PostCommentListPopupState extends State<PostCommentListPopup> {
final GlobalKey<PostCommentSliverListState> _childListKey = GlobalKey();
2024-11-11 12:06:00 +00:00
@override
Widget build(BuildContext context) {
2024-11-14 14:49:17 +00:00
final ua = context.watch<UserProvider>();
2024-11-11 14:43:09 +00:00
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
2024-11-11 12:06:00 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Symbols.comment, size: 24),
const Gap(16),
Text('postCommentsDetailed')
2024-11-11 14:43:09 +00:00
.plural(widget.commentCount)
2024-11-11 12:06:00 +00:00
.textStyle(Theme.of(context).textTheme.titleLarge!),
],
).padding(horizontal: 20, top: 16, bottom: 12),
Expanded(
child: CustomScrollView(
slivers: [
2024-11-14 14:49:17 +00:00
if (ua.isAuthorized)
SliverToBoxAdapter(
child: Container(
height: 240,
decoration: BoxDecoration(
border: Border.symmetric(
horizontal: BorderSide(
color: Theme.of(context).dividerColor,
width: 1 / devicePixelRatio,
),
2024-11-11 14:43:09 +00:00
),
),
2024-11-14 14:49:17 +00:00
child: PostMiniEditor(
postReplyId: widget.postId,
onPost: () {
_childListKey.currentState!.refresh();
},
),
2024-11-11 14:43:09 +00:00
),
),
PostCommentSliverList(
key: _childListKey,
parentPostId: widget.postId,
),
2024-11-11 12:06:00 +00:00
],
),
),
],
);
}
}