Solian/lib/screens/feed.dart

108 lines
3.2 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-25 16:11:00 +00:00
import 'package:solian/providers/content/post.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-06-22 15:59:11 +00:00
import 'package:solian/widgets/app_bar_title.dart';
2024-06-06 12:23:50 +00:00
import 'package:solian/widgets/current_state_action.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-07-06 12:55:53 +00:00
class FeedScreen extends StatefulWidget {
const FeedScreen({super.key});
2024-05-18 10:17:16 +00:00
@override
2024-07-06 12:55:53 +00:00
State<FeedScreen> createState() => _FeedScreenState();
2024-05-18 10:17:16 +00:00
}
2024-07-06 12:55:53 +00:00
class _FeedScreenState extends State<FeedScreen> {
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
return Scaffold(
body: Material(
color: Theme.of(context).colorScheme.surface,
2024-05-29 12:13:53 +00:00
child: RefreshIndicator(
onRefresh: () => Future.sync(() => _pagingController.refresh()),
child: CustomScrollView(
slivers: [
SliverAppBar(
2024-07-06 12:55:53 +00:00
title: AppBarTitle('feed'.tr),
2024-05-29 12:13:53 +00:00
centerTitle: false,
floating: true,
2024-06-22 15:59:11 +00:00
titleSpacing: SolianTheme.titleSpacing(context),
toolbarHeight: SolianTheme.toolbarHeight(context),
2024-05-29 12:13:53 +00:00
actions: [
2024-06-06 12:23:50 +00:00
const BackgroundStateWidget(),
2024-05-29 12:13:53 +00:00
const NotificationButton(),
2024-07-06 12:55:53 +00:00
const FeedCreationButton(),
2024-05-29 12:13:53 +00:00
SizedBox(
width: SolianTheme.isLargeScreen(context) ? 8 : 16,
),
2024-05-29 12:13:53 +00:00
],
2024-05-25 05:00:40 +00:00
),
2024-05-29 12:13:53 +00:00
PostListWidget(controller: _pagingController),
],
2024-05-19 10:01:00 +00:00
),
2024-05-18 16:56:32 +00:00
),
2024-05-18 10:17:16 +00:00
),
);
}
}
2024-07-06 12:55:53 +00:00
class FeedCreationButton extends StatelessWidget {
const FeedCreationButton({super.key});
@override
Widget build(BuildContext context) {
final AuthProvider auth = Get.find();
return FutureBuilder(
future: auth.isAuthorized,
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data == true) {
return IconButton(
icon: const Icon(Icons.add_circle),
onPressed: () {
AppRouter.instance.pushNamed('postPublishing');
},
);
}
return const SizedBox();
});
}
}