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';
|
|
|
|
|
2024-03-24 05:34:29 +00:00
|
|
|
class CommentList extends StatefulWidget {
|
2024-03-24 04:12:13 +00:00
|
|
|
final Feed parent;
|
|
|
|
|
2024-03-24 05:34:29 +00:00
|
|
|
const CommentList({super.key, required this.parent});
|
2024-03-24 04:12:13 +00:00
|
|
|
|
|
|
|
@override
|
2024-03-24 05:34:29 +00:00
|
|
|
State<CommentList> createState() => _CommentListState();
|
2024-03-24 04:12:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 05:34:29 +00:00
|
|
|
class _CommentListState extends State<CommentList> {
|
2024-03-24 04:12:13 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|