2024-07-07 06:22:53 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
|
|
|
import 'package:solian/models/pagination.dart';
|
2024-07-23 10:09:41 +00:00
|
|
|
import 'package:solian/providers/content/posts.dart';
|
2024-07-31 17:21:27 +00:00
|
|
|
import 'package:solian/widgets/posts/post_warped_list.dart';
|
2024-07-07 06:22:53 +00:00
|
|
|
|
2024-07-23 10:09:41 +00:00
|
|
|
import '../../models/post.dart';
|
|
|
|
|
2024-07-07 06:22:53 +00:00
|
|
|
class FeedSearchScreen extends StatefulWidget {
|
|
|
|
final String? tag;
|
|
|
|
final String? category;
|
|
|
|
|
|
|
|
const FeedSearchScreen({super.key, this.tag, this.category});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<FeedSearchScreen> createState() => _FeedSearchScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _FeedSearchScreenState extends State<FeedSearchScreen> {
|
2024-07-23 10:09:41 +00:00
|
|
|
final PagingController<int, Post> _pagingController =
|
2024-07-07 06:22:53 +00:00
|
|
|
PagingController(firstPageKey: 0);
|
|
|
|
|
|
|
|
getPosts(int pageKey) async {
|
2024-07-23 10:09:41 +00:00
|
|
|
final PostProvider provider = Get.find();
|
2024-07-07 06:22:53 +00:00
|
|
|
|
|
|
|
Response resp;
|
|
|
|
try {
|
2024-07-25 17:31:45 +00:00
|
|
|
resp = await provider.listPost(
|
2024-07-07 06:22:53 +00:00
|
|
|
pageKey,
|
|
|
|
tag: widget.tag,
|
|
|
|
category: widget.category,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
_pagingController.error = e;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final PaginationResult result = PaginationResult.fromJson(resp.body);
|
2024-07-23 10:09:41 +00:00
|
|
|
final parsed = result.data?.map((e) => Post.fromJson(e)).toList();
|
2024-07-07 06:22:53 +00:00
|
|
|
if (parsed != null && parsed.length >= 10) {
|
|
|
|
_pagingController.appendPage(parsed, pageKey + parsed.length);
|
|
|
|
} else if (parsed != null) {
|
|
|
|
_pagingController.appendLastPage(parsed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
_pagingController.addPageRequestListener(getPosts);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
body: Material(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
if (widget.tag != null)
|
|
|
|
ListTile(
|
|
|
|
leading: const Icon(Icons.label),
|
|
|
|
tileColor: Theme.of(context).colorScheme.surfaceContainer,
|
|
|
|
title: Text('feedSearchWithTag'.trParams({'key': widget.tag!})),
|
|
|
|
),
|
|
|
|
if (widget.category != null)
|
|
|
|
ListTile(
|
|
|
|
leading: const Icon(Icons.category),
|
|
|
|
tileColor: Theme.of(context).colorScheme.surfaceContainer,
|
2024-07-09 14:39:44 +00:00
|
|
|
title: Text('feedSearchWithCategory'
|
|
|
|
.trParams({'key': widget.category!})),
|
2024-07-07 06:22:53 +00:00
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: RefreshIndicator(
|
|
|
|
onRefresh: () => Future.sync(() => _pagingController.refresh()),
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: [
|
2024-07-31 17:21:27 +00:00
|
|
|
PostWarpedListWidget(controller: _pagingController),
|
2024-07-07 06:22:53 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-07-09 14:39:44 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_pagingController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2024-07-07 06:22:53 +00:00
|
|
|
}
|