Surface/lib/screens/explore.dart

189 lines
6.0 KiB
Dart
Raw Normal View History

2024-11-08 16:09:46 +00:00
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
2024-11-09 17:34:58 +00:00
import 'package:flutter_expandable_fab/flutter_expandable_fab.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:material_symbols_icons/symbols.dart';
2024-11-08 16:09:46 +00:00
import 'package:provider/provider.dart';
2024-11-25 16:00:09 +00:00
import 'package:surface/providers/post.dart';
2024-11-08 16:09:46 +00:00
import 'package:surface/types/post.dart';
2024-11-09 03:16:14 +00:00
import 'package:surface/widgets/post/post_item.dart';
2024-11-08 16:09:46 +00:00
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
class ExploreScreen extends StatefulWidget {
const ExploreScreen({super.key});
@override
State<ExploreScreen> createState() => _ExploreScreenState();
}
class _ExploreScreenState extends State<ExploreScreen> {
2024-11-09 17:34:58 +00:00
final _fabKey = GlobalKey<ExpandableFabState>();
2024-11-08 16:09:46 +00:00
bool _isBusy = true;
final List<SnPost> _posts = List.empty(growable: true);
2024-11-09 03:16:14 +00:00
int? _postCount;
2024-11-08 16:09:46 +00:00
2024-11-10 10:37:34 +00:00
Future<void> _fetchPosts() async {
2024-11-09 03:16:14 +00:00
if (_postCount != null && _posts.length >= _postCount!) return;
2024-11-08 16:09:46 +00:00
setState(() => _isBusy = true);
2024-11-25 16:00:09 +00:00
final pt = context.read<SnPostContentProvider>();
final result = await pt.listPosts(take: 10, offset: _posts.length);
final out = result.$1;
2024-11-09 04:04:03 +00:00
if (!mounted) return;
2024-11-08 16:09:46 +00:00
2024-11-25 16:00:09 +00:00
_postCount = result.$2;
2024-11-09 04:04:03 +00:00
_posts.addAll(out);
2024-11-08 16:09:46 +00:00
if (mounted) setState(() => _isBusy = false);
}
@override
void initState() {
super.initState();
_fetchPosts();
}
@override
Widget build(BuildContext context) {
return Scaffold(
2024-11-09 17:34:58 +00:00
floatingActionButtonLocation: ExpandableFab.location,
floatingActionButton: ExpandableFab(
key: _fabKey,
distance: 75,
type: ExpandableFabType.up,
childrenAnimation: ExpandableFabAnimation.none,
2024-11-10 08:41:11 +00:00
overlayStyle: ExpandableFabOverlayStyle(
color: Theme.of(context)
.colorScheme
.surface
.withAlpha((255 * 0.5).round()),
),
2024-11-09 17:34:58 +00:00
openButtonBuilder: RotateFloatingActionButtonBuilder(
child: const Icon(Symbols.add, size: 28),
fabSize: ExpandableFabSize.regular,
foregroundColor:
Theme.of(context).floatingActionButtonTheme.foregroundColor,
backgroundColor:
Theme.of(context).floatingActionButtonTheme.backgroundColor,
shape: const CircleBorder(),
),
closeButtonBuilder: DefaultFloatingActionButtonBuilder(
child: const Icon(Symbols.close, size: 28),
fabSize: ExpandableFabSize.regular,
foregroundColor:
Theme.of(context).floatingActionButtonTheme.foregroundColor,
backgroundColor:
Theme.of(context).floatingActionButtonTheme.backgroundColor,
shape: const CircleBorder(),
),
children: [
Row(
children: [
Text('writePostTypeStory').tr(),
const Gap(20),
FloatingActionButton(
heroTag: null,
tooltip: 'writePostTypeStory'.tr(),
onPressed: () {
GoRouter.of(context).pushNamed('postEditor', pathParameters: {
2024-11-10 04:41:56 +00:00
'mode': 'stories',
2024-11-09 17:34:58 +00:00
}).then((value) {
if (value == true) {
_posts.clear();
_fetchPosts();
}
});
_fabKey.currentState!.toggle();
},
child: const Icon(Symbols.post_rounded),
),
],
),
Row(
children: [
Text('writePostTypeArticle').tr(),
const Gap(20),
FloatingActionButton(
heroTag: null,
tooltip: 'writePostTypeArticle'.tr(),
onPressed: () {
GoRouter.of(context).pushNamed('postEditor', pathParameters: {
2024-11-10 04:41:56 +00:00
'mode': 'articles',
2024-11-09 17:34:58 +00:00
}).then((value) {
if (value == true) {
_posts.clear();
_fetchPosts();
}
});
_fabKey.currentState!.toggle();
},
child: const Icon(Symbols.news),
),
],
),
],
),
2024-11-10 10:37:34 +00:00
body: RefreshIndicator(
displacement: 40 + MediaQuery.of(context).padding.top,
onRefresh: () {
_posts.clear();
return _fetchPosts();
2024-11-08 16:09:46 +00:00
},
2024-11-10 10:37:34 +00:00
child: CustomScrollView(
slivers: [
SliverAppBar(
title: Text('screenExplore').tr(),
floating: true,
snap: true,
2024-11-23 11:06:09 +00:00
actions: [
IconButton(
icon: const Icon(Symbols.search),
onPressed: () {
GoRouter.of(context).pushNamed('postSearch');
},
),
2024-11-24 12:23:06 +00:00
const Gap(8),
2024-11-23 11:06:09 +00:00
],
2024-11-10 10:37:34 +00:00
),
SliverInfiniteList(
itemCount: _posts.length,
isLoading: _isBusy,
2024-11-11 12:06:00 +00:00
centerLoading: true,
2024-11-10 10:37:34 +00:00
hasReachedMax: _postCount != null && _posts.length >= _postCount!,
onFetchData: _fetchPosts,
itemBuilder: (context, idx) {
2024-11-10 14:56:09 +00:00
return GestureDetector(
2024-11-23 10:04:30 +00:00
child: PostItem(
data: _posts[idx],
maxWidth: 640,
onChanged: (data) {
setState(() => _posts[idx] = data);
},
2024-11-28 14:04:38 +00:00
onDeleted: () {
_posts.clear();
_fetchPosts();
},
2024-11-23 10:04:30 +00:00
),
2024-11-10 14:56:09 +00:00
onTap: () {
GoRouter.of(context).pushNamed(
'postDetail',
pathParameters: {'slug': _posts[idx].id.toString()},
extra: _posts[idx],
);
},
);
2024-11-10 10:37:34 +00:00
},
2024-11-11 12:06:00 +00:00
separatorBuilder: (context, index) => const Divider(height: 1),
),
2024-11-10 10:37:34 +00:00
],
),
2024-11-08 16:09:46 +00:00
),
);
}
}