Solian/lib/screens/social.dart

109 lines
3.4 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';
2024-05-25 05:00:40 +00:00
import 'package:solian/screens/account/notification.dart';
import 'package:solian/theme.dart';
2024-05-25 09:21:27 +00:00
import 'package:solian/widgets/posts/post_list.dart';
2024-05-18 10:17:16 +00:00
2024-05-23 12:00:26 +00:00
class SocialScreen extends StatefulWidget {
const SocialScreen({super.key});
2024-05-18 10:17:16 +00:00
@override
2024-05-23 12:00:26 +00:00
State<SocialScreen> createState() => _SocialScreenState();
2024-05-18 10:17:16 +00:00
}
2024-05-23 12:00:26 +00:00
class _SocialScreenState extends State<SocialScreen> {
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-25 05:19:16 +00:00
final PostProvider provider = Get.find();
Response resp;
try {
resp = await provider.listPost(pageKey);
} catch (e) {
_pagingController.error = e;
2024-05-19 10:01:00 +00:00
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() {
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-25 05:00:40 +00:00
child: SafeArea(
child: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
SliverOverlapAbsorber(
handle:
NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
title: Text('social'.tr),
centerTitle: false,
titleSpacing:
SolianTheme.isLargeScreen(context) ? null : 24,
forceElevated: innerBoxIsScrolled,
actions: const [
NotificationButton(),
],
),
2024-05-25 05:00:40 +00:00
),
];
},
body: MediaQuery.removePadding(
removeTop: true,
context: context,
child: RefreshIndicator(
onRefresh: () => Future.sync(() => _pagingController.refresh()),
2024-05-25 09:21:27 +00:00
child: PostListWidget(controller: _pagingController),
2024-05-25 05:00:40 +00:00
),
2024-05-19 10:01:00 +00:00
),
),
2024-05-18 16:56:32 +00:00
),
2024-05-18 10:17:16 +00:00
),
);
}
}