Solian/lib/screens/feed/search.dart

100 lines
2.9 KiB
Dart
Raw Normal View History

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';
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
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> {
final PagingController<int, Post> _pagingController =
2024-07-07 06:22:53 +00:00
PagingController(firstPageKey: 0);
getPosts(int pageKey) async {
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);
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-08-04 10:13:59 +00:00
PostWarpedListWidget(
controller: _pagingController,
onUpdate: () => _pagingController.refresh(),
),
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
}