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-21 16:05:03 +00:00
|
|
|
import 'package:solian/widgets/posts/post_action.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/widgets/posts/post_item.dart';
|
|
|
|
|
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> {
|
2024-05-21 16:05:03 +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-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() {
|
2024-05-25 05:19:16 +00:00
|
|
|
Get.lazyPut(() => PostProvider());
|
2024-05-18 10:17:16 +00:00
|
|
|
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 {
|
2024-05-21 16:05:03 +00:00
|
|
|
final value =
|
|
|
|
await AppRouter.instance.pushNamed('postPublishing');
|
2024-05-19 10:01:00 +00:00
|
|
|
if (value != null) {
|
|
|
|
_pagingController.refresh();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Container();
|
|
|
|
}),
|
|
|
|
body: Material(
|
2024-05-21 16:05:03 +00:00
|
|
|
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-21 16:05:03 +00:00
|
|
|
),
|
2024-05-25 05:00:40 +00:00
|
|
|
),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
body: MediaQuery.removePadding(
|
|
|
|
removeTop: true,
|
|
|
|
context: context,
|
|
|
|
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,
|
2024-05-25 05:19:16 +00:00
|
|
|
isClickable: true,
|
2024-05-25 05:00:40 +00:00
|
|
|
).paddingSymmetric(
|
|
|
|
vertical:
|
|
|
|
(item.attachments?.isEmpty ?? false) ? 8 : 0,
|
|
|
|
),
|
2024-05-25 05:19:16 +00:00
|
|
|
onTap: () {
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'postDetail',
|
|
|
|
pathParameters: {'alias': item.alias},
|
|
|
|
);
|
|
|
|
},
|
2024-05-25 05:00:40 +00:00
|
|
|
onLongPress: () {
|
|
|
|
showModalBottomSheet(
|
|
|
|
useRootNavigator: true,
|
|
|
|
context: context,
|
|
|
|
builder: (context) => PostAction(item: item),
|
|
|
|
).then((value) {
|
|
|
|
if (value == true) _pagingController.refresh();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|