Surface/lib/screens/post/post_detail.dart

175 lines
5.5 KiB
Dart
Raw Normal View History

2024-11-10 14:56:09 +00:00
import 'dart:math' as math;
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
2024-11-11 12:06:00 +00:00
import 'package:gap/gap.dart';
2024-11-10 14:56:09 +00:00
import 'package:go_router/go_router.dart';
2024-11-11 14:43:09 +00:00
import 'package:material_symbols_icons/symbols.dart';
2024-11-10 14:56:09 +00:00
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-10 14:56:09 +00:00
import 'package:surface/types/post.dart';
import 'package:surface/widgets/dialog.dart';
import 'package:surface/widgets/loading_indicator.dart';
2024-11-11 12:06:00 +00:00
import 'package:surface/widgets/post/post_comment_list.dart';
2024-11-10 14:56:09 +00:00
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-10 14:56:09 +00:00
class PostDetailScreen extends StatefulWidget {
final String slug;
final SnPost? preload;
const PostDetailScreen({
super.key,
required this.slug,
this.preload,
});
@override
State<PostDetailScreen> createState() => _PostDetailScreenState();
}
class _PostDetailScreenState extends State<PostDetailScreen> {
bool _isBusy = false;
SnPost? _data;
void _fetchPost() async {
setState(() => _isBusy = true);
try {
2024-11-25 16:00:09 +00:00
final pt = context.read<SnPostContentProvider>();
final post = await pt.getPost(widget.slug);
2024-11-10 14:56:09 +00:00
if (!mounted) return;
2024-11-25 16:00:09 +00:00
_data = post;
2024-11-10 14:56:09 +00:00
} catch (err) {
context.showErrorDialog(err);
} finally {
setState(() => _isBusy = false);
}
}
@override
void initState() {
super.initState();
if (widget.preload != null) {
_data = widget.preload;
}
_fetchPost();
}
2024-11-11 14:43:09 +00:00
final GlobalKey<PostCommentSliverListState> _childListKey = GlobalKey();
2024-11-10 14:56:09 +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;
return Scaffold(
2024-11-10 14:56:09 +00:00
appBar: AppBar(
leading: BackButton(
onPressed: () {
if (GoRouter.of(context).canPop()) {
2024-11-23 11:06:09 +00:00
GoRouter.of(context).pop(context);
return;
2024-11-10 14:56:09 +00:00
}
GoRouter.of(context).replaceNamed('explore');
},
),
title: _data?.body['title'] != null
? RichText(
textAlign: TextAlign.center,
text: TextSpan(children: [
TextSpan(
text: _data?.body['title'] ?? 'postNoun'.tr(),
style: Theme.of(context)
.textTheme
.titleLarge!
.copyWith(color: Colors.white),
),
const TextSpan(text: '\n'),
TextSpan(
text: 'postDetail'.tr(),
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(color: Colors.white),
),
]),
)
: Text('postDetail').tr(),
2024-11-10 14:56:09 +00:00
),
2024-11-11 12:06:00 +00:00
body: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: LoadingIndicator(isActive: _isBusy),
),
if (_data != null)
SliverToBoxAdapter(
2024-11-21 14:55:00 +00:00
child: PostItem(
data: _data!,
maxWidth: 640,
showComments: false,
2024-11-23 10:04:30 +00:00
onChanged: (data) {
setState(() => _data = data);
},
2024-11-21 14:55:00 +00:00
),
2024-11-11 12:06:00 +00:00
),
const SliverToBoxAdapter(child: Divider(height: 1)),
if (_data != null)
SliverToBoxAdapter(
2024-11-23 16:22:08 +00:00
child: Container(
constraints: const BoxConstraints(maxWidth: 640),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Symbols.comment, size: 24),
const Gap(16),
Text('postCommentsDetailed')
.plural(_data!.metric.replyCount)
.textStyle(Theme.of(context).textTheme.titleLarge!),
],
).padding(horizontal: 20, vertical: 12).center(),
),
2024-11-11 14:43:09 +00:00
),
2024-11-14 14:49:17 +00:00
if (_data != null && ua.isAuthorized)
2024-11-11 14:43:09 +00:00
SliverToBoxAdapter(
child: Container(
height: 240,
2024-11-23 16:22:08 +00:00
constraints: const BoxConstraints(maxWidth: 640),
2024-11-11 14:43:09 +00:00
decoration: BoxDecoration(
border: Border.symmetric(
horizontal: BorderSide(
color: Theme.of(context).dividerColor,
width: 1 / devicePixelRatio,
),
),
),
child: PostMiniEditor(
postReplyId: _data!.id,
onPost: () {
2024-11-12 12:47:40 +00:00
setState(() {
_data = _data!.copyWith(
metric: _data!.metric.copyWith(
replyCount: _data!.metric.replyCount + 1,
),
);
});
2024-11-23 10:04:30 +00:00
_childListKey.currentState!.refresh();
2024-11-11 14:43:09 +00:00
},
),
2024-11-23 16:22:08 +00:00
).center(),
2024-11-11 14:43:09 +00:00
),
if (_data != null)
PostCommentSliverList(
key: _childListKey,
parentPostId: _data!.id,
2024-11-14 14:49:17 +00:00
maxWidth: 640,
2024-11-11 12:06:00 +00:00
),
SliverGap(math.max(MediaQuery.of(context).padding.bottom, 16)),
],
2024-11-10 14:56:09 +00:00
),
);
}
}