Solian/lib/screens/home.dart

104 lines
3.5 KiB
Dart
Raw Normal View History

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';
import 'package:solian/widgets/posts/post_action.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> {
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');
2024-05-19 10:01:00 +00:00
if (value != null) {
_pagingController.refresh();
}
},
);
}
return Container();
}),
body: Material(
color: Theme.of(context).colorScheme.surface,
2024-05-19 10:01:00 +00:00
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)
.paddingSymmetric(
vertical: (item.attachments?.isEmpty ?? false) ? 8 : 0,
),
2024-05-19 10:01:00 +00:00
onTap: () {},
onLongPress: () {
showModalBottomSheet(
useRootNavigator: true,
context: context,
builder: (context) => PostAction(item: item),
).then((value) {
if (value == true) _pagingController.refresh();
});
},
2024-05-19 10:01:00 +00:00
);
},
),
separatorBuilder: (_, __) =>
const Divider(thickness: 0.3, height: 0.3),
2024-05-19 10:01:00 +00:00
),
2024-05-18 16:56:32 +00:00
),
2024-05-18 10:17:16 +00:00
),
);
}
}