This repository has been archived on 2024-06-08. You can view files and clone it, but cannot push or open issues or pull requests.
SolarAgent/lib/widgets/posts/comments.dart

130 lines
3.8 KiB
Dart
Raw Normal View History

2024-03-24 04:12:13 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
2024-03-24 04:22:29 +00:00
import 'package:solaragent/auth.dart';
2024-03-24 04:12:13 +00:00
import 'package:solaragent/models/feed.dart';
import 'package:solaragent/models/pagination.dart';
2024-03-24 04:22:29 +00:00
import 'package:solaragent/router.dart';
2024-03-24 04:12:13 +00:00
import 'package:solaragent/widgets/feed.dart';
class CommentListWidget extends StatefulWidget {
final Feed parent;
const CommentListWidget({super.key, required this.parent});
@override
State<CommentListWidget> createState() => _CommentListWidgetState();
}
class _CommentListWidgetState extends State<CommentListWidget> {
static const pageSize = 5;
final client = Client();
final PagingController<int, Feed> paginationController =
PagingController(firstPageKey: 0);
List<Feed> feed = List.empty();
@override
void initState() {
super.initState();
paginationController.addPageRequestListener((pageKey) {
pullFeed(pageKey);
});
}
Future<void> pullFeed(int pageKey) async {
var offset = pageKey;
var take = pageSize;
var dataset = "${widget.parent.modelType}s";
var alias = widget.parent.alias;
var uri = Uri.parse(
'https://co.solsynth.dev/api/p/$dataset/$alias/comments?take=$take&offset=$offset',
);
var res = await client.get(uri);
if (res.statusCode == 200) {
final result =
PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
final isLastPage = (result.count - pageKey) < pageSize;
final items =
result.data?.map((x) => Feed.fromJson(x)).toList() ?? List.empty();
if (isLastPage || result.data == null) {
paginationController.appendLastPage(items);
} else {
final nextPageKey = pageKey + items.length;
paginationController.appendPage(items, nextPageKey);
}
} else {
paginationController.error = utf8.decode(res.bodyBytes);
}
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
padding: const EdgeInsets.only(left: 10, right: 10, top: 20),
2024-03-24 04:22:29 +00:00
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8.0,
vertical: 12.0,
),
child: Text(
'Comments',
style: Theme.of(context).textTheme.headlineSmall,
),
),
FutureBuilder(
future: authClient.isAuthorized(),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data == true) {
return TextButton.icon(
icon: const Icon(Icons.edit),
label: const Text("LEAVE COMMENT"),
onPressed: () {
2024-03-24 04:32:24 +00:00
router
.push("/post/comments", extra: widget.parent)
.then((value) {
if (value == true) paginationController.refresh();
});
2024-03-24 04:22:29 +00:00
},
);
} else {
return Container();
}
},
),
],
2024-03-24 04:12:13 +00:00
),
),
Expanded(
child: PagedListView<int, Feed>(
pagingController: paginationController,
builderDelegate: PagedChildBuilderDelegate<Feed>(
itemBuilder: (context, item, index) => FeedItem(
item: item,
),
),
),
),
],
);
}
@override
void dispose() {
paginationController.dispose();
super.dispose();
}
}