Solian/lib/screens/posts/screen.dart

103 lines
2.9 KiB
Dart
Raw Normal View History

2024-04-13 17:07:57 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:solian/models/post.dart';
import 'package:solian/utils/service_url.dart';
2024-04-29 12:22:06 +00:00
import 'package:solian/widgets/exts.dart';
import 'package:solian/widgets/scaffold.dart';
2024-04-13 17:07:57 +00:00
import 'package:solian/widgets/posts/comment_list.dart';
import 'package:solian/widgets/posts/post.dart';
2024-04-13 17:07:57 +00:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2024-05-01 16:49:38 +00:00
class PostScreen extends StatelessWidget {
2024-04-13 17:07:57 +00:00
final String dataset;
final String alias;
const PostScreen({super.key, required this.alias, required this.dataset});
@override
2024-05-01 16:49:38 +00:00
Widget build(BuildContext context) {
return IndentScaffold(
2024-05-01 16:49:38 +00:00
title: AppLocalizations.of(context)!.post,
hideDrawer: true,
2024-05-08 14:01:06 +00:00
body: PostScreenWidget(
2024-05-01 16:49:38 +00:00
dataset: dataset,
alias: alias,
),
);
}
2024-04-13 17:07:57 +00:00
}
2024-05-01 16:49:38 +00:00
class PostScreenWidget extends StatefulWidget {
final String dataset;
final String alias;
const PostScreenWidget({super.key, required this.dataset, required this.alias});
@override
State<PostScreenWidget> createState() => _PostScreenWidgetState();
}
class _PostScreenWidgetState extends State<PostScreenWidget> {
2024-04-13 17:07:57 +00:00
final _client = http.Client();
2024-05-01 16:49:38 +00:00
final PagingController<int, Post> _commentPagingController = PagingController(firstPageKey: 0);
2024-04-13 17:07:57 +00:00
Future<Post?> fetchPost(BuildContext context) async {
2024-05-01 16:49:38 +00:00
final uri = getRequestUri('interactive', '/api/p/${widget.dataset}/${widget.alias}');
2024-04-13 17:07:57 +00:00
final res = await _client.get(uri);
if (res.statusCode != 200) {
final err = utf8.decode(res.bodyBytes);
2024-04-29 12:22:06 +00:00
context.showErrorDialog(err);
2024-04-13 17:07:57 +00:00
return null;
} else {
return Post.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
}
}
@override
Widget build(BuildContext context) {
2024-05-01 16:49:38 +00:00
return FutureBuilder(
future: fetchPost(context),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data != null) {
return CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: PostItem(
item: snapshot.data!,
brief: false,
ripple: false,
2024-04-13 17:07:57 +00:00
),
2024-05-01 16:49:38 +00:00
),
SliverToBoxAdapter(
child: CommentListHeader(
2024-04-25 15:03:16 +00:00
related: snapshot.data!,
paging: _commentPagingController,
),
2024-05-01 16:49:38 +00:00
),
CommentList(
related: snapshot.data!,
dataset: widget.dataset,
paging: _commentPagingController,
),
],
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
2024-04-13 17:07:57 +00:00
);
}
@override
void dispose() {
_commentPagingController.dispose();
super.dispose();
}
}