99 lines
2.6 KiB
Dart
99 lines
2.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
|
import 'package:solaragent/models/feed.dart';
|
|
import 'package:solaragent/models/pagination.dart';
|
|
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),
|
|
child: ListTile(
|
|
title: Text(
|
|
'Comments',
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: PagedListView<int, Feed>(
|
|
pagingController: paginationController,
|
|
builderDelegate: PagedChildBuilderDelegate<Feed>(
|
|
itemBuilder: (context, item, index) => FeedItem(
|
|
item: item,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
paginationController.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|