2024-05-18 10:17:16 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
2024-05-19 10:01:00 +00:00
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/models/pagination.dart';
|
|
|
|
import 'package:solian/models/post.dart';
|
2024-05-19 10:01:00 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/providers/content/post_explore.dart';
|
2024-05-19 10:01:00 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/widgets/posts/post_item.dart';
|
|
|
|
|
|
|
|
class HomeScreen extends StatefulWidget {
|
|
|
|
const HomeScreen({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
2024-05-19 10:01:00 +00:00
|
|
|
final PagingController<int, Post> _pagingController = PagingController(firstPageKey: 0);
|
2024-05-18 10:17:16 +00:00
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
getPosts(int pageKey) async {
|
2024-05-18 10:17:16 +00:00
|
|
|
final PostExploreProvider provider = Get.find();
|
2024-05-19 10:01:00 +00:00
|
|
|
final resp = await provider.listPost(pageKey);
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
_pagingController.error = resp.bodyString;
|
|
|
|
return;
|
|
|
|
}
|
2024-05-18 10:17:16 +00:00
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
final PaginationResult result = PaginationResult.fromJson(resp.body);
|
|
|
|
final parsed = result.data?.map((e) => Post.fromJson(e)).toList();
|
|
|
|
if (parsed != null && parsed.length >= 10) {
|
|
|
|
_pagingController.appendPage(parsed, pageKey + parsed.length);
|
|
|
|
} else if (parsed != null) {
|
|
|
|
_pagingController.appendLastPage(parsed);
|
|
|
|
}
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
Get.lazyPut(() => PostExploreProvider());
|
|
|
|
super.initState();
|
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
_pagingController.addPageRequestListener(getPosts);
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-19 10:01:00 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
2024-05-18 10:17:16 +00:00
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
return Scaffold(
|
|
|
|
floatingActionButton: FutureBuilder(
|
|
|
|
future: auth.isAuthorized,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData && snapshot.data == true) {
|
|
|
|
return FloatingActionButton(
|
|
|
|
child: const Icon(Icons.add),
|
|
|
|
onPressed: () async {
|
|
|
|
final value = await AppRouter.instance.pushNamed('postPublishing');
|
|
|
|
if (value != null) {
|
|
|
|
_pagingController.refresh();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Container();
|
|
|
|
}),
|
|
|
|
body: Material(
|
|
|
|
color: Theme.of(context).colorScheme.background,
|
|
|
|
child: RefreshIndicator(
|
|
|
|
onRefresh: () => Future.sync(() => _pagingController.refresh()),
|
|
|
|
child: PagedListView<int, Post>.separated(
|
|
|
|
pagingController: _pagingController,
|
|
|
|
builderDelegate: PagedChildBuilderDelegate<Post>(
|
|
|
|
itemBuilder: (context, item, index) {
|
|
|
|
return GestureDetector(
|
|
|
|
child: PostItem(key: Key('p${item.alias}'), item: item),
|
|
|
|
onTap: () {},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
separatorBuilder: (_, __) => const Divider(thickness: 0.3, height: 0.3),
|
|
|
|
),
|
2024-05-18 16:56:32 +00:00
|
|
|
),
|
2024-05-18 10:17:16 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|