Solian/lib/screens/explore.dart

252 lines
9.7 KiB
Dart
Raw Normal View History

import 'dart:async';
2024-10-12 17:31:59 +00:00
import 'dart:ui';
2024-05-18 10:17:16 +00:00
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
2024-05-18 10:17:16 +00:00
import 'package:get/get.dart';
2024-07-25 06:42:50 +00:00
import 'package:solian/controllers/post_list_controller.dart';
2024-05-19 10:01:00 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/navigation.dart';
2024-10-13 13:48:53 +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';
import 'package:solian/widgets/account/signin_required_overlay.dart';
2024-06-06 12:23:50 +00:00
import 'package:solian/widgets/current_state_action.dart';
import 'package:solian/widgets/app_bar_leading.dart';
import 'package:solian/widgets/navigation/realm_switcher.dart';
2024-10-11 16:41:03 +00:00
import 'package:solian/widgets/posts/post_creation.dart';
2024-10-10 14:52:05 +00:00
import 'package:solian/widgets/posts/post_list.dart';
2024-07-25 08:08:46 +00:00
import 'package:solian/widgets/posts/post_shuffle_swiper.dart';
2024-10-06 09:37:07 +00:00
import 'package:solian/widgets/root_container.dart';
2024-05-18 10:17:16 +00:00
class ExploreScreen extends StatefulWidget {
const ExploreScreen({super.key});
2024-05-18 10:17:16 +00:00
@override
State<ExploreScreen> createState() => _ExploreScreenState();
2024-05-18 10:17:16 +00:00
}
class _ExploreScreenState extends State<ExploreScreen>
2024-07-24 18:00:29 +00:00
with SingleTickerProviderStateMixin {
2024-07-25 08:08:46 +00:00
late final PostListController _postController;
2024-07-24 18:00:29 +00:00
late final TabController _tabController;
List<StreamSubscription>? _subscriptions;
2024-05-18 10:17:16 +00:00
@override
void initState() {
super.initState();
final navState = Get.find<NavigationStateProvider>();
2024-07-26 06:21:00 +00:00
_postController = PostListController();
_postController.realm = navState.focusedRealm.value?.alias;
_tabController = TabController(length: 3, vsync: this);
2024-07-24 18:00:29 +00:00
_tabController.addListener(() {
if (_postController.mode.value == _tabController.index) return;
_postController.mode.value = _tabController.index;
_postController.reloadAllOver();
2024-07-24 18:00:29 +00:00
});
_subscriptions = [
Get.find<NavigationStateProvider>().focusedRealm.listen((value) {
if (value?.alias != _postController.realm) {
_postController.realm = value?.alias;
_postController.reloadAllOver();
}
}),
];
2024-05-18 10:17:16 +00:00
}
@override
Widget build(BuildContext context) {
final AuthProvider auth = Get.find();
2024-10-06 09:37:07 +00:00
return RootContainer(
child: Scaffold(
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
2024-08-04 10:13:59 +00:00
onPressed: () async {
final value = await showModalBottomSheet(
useRootNavigator: true,
isScrollControlled: true,
context: context,
2024-08-04 10:13:59 +00:00
builder: (context) => const PostCreatePopup(),
);
2024-08-04 10:13:59 +00:00
if (value is Future) {
value.then((_) {
_postController.reloadAllOver();
});
} else if (value != null) {
_postController.reloadAllOver();
}
},
),
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverLayoutBuilder(
builder: (context, constraints) {
final scrollOffset = constraints.scrollOffset;
final colorChangeOffset = 120;
final scrollProgress =
(scrollOffset / colorChangeOffset).clamp(0.0, 1.0);
2024-10-12 17:31:59 +00:00
final blurSigma = lerpDouble(0, 10, scrollProgress) ?? 0;
return SliverAppBar(
2024-10-12 17:31:59 +00:00
flexibleSpace: ClipRRect(
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: blurSigma,
sigmaY: blurSigma,
),
child: ListView(
padding: EdgeInsets.zero,
physics: const NeverScrollableScrollPhysics(),
children: [
SizedBox(
height: 48,
child: const Row(
children: [
RealmSwitcher(),
],
).paddingSymmetric(horizontal: 8),
2024-10-13 11:56:37 +00:00
).paddingSymmetric(vertical: 4),
2024-10-12 17:31:59 +00:00
TabBar(
controller: _tabController,
2024-10-13 11:56:37 +00:00
dividerHeight: scrollProgress > 0 ? 0 : 0.3,
2024-10-12 17:31:59 +00:00
tabAlignment: TabAlignment.fill,
tabs: [
Tab(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.feed, size: 20),
const Gap(8),
Text('postListNews'.tr),
],
),
),
Tab(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.people, size: 20),
const Gap(8),
Text('postListFriends'.tr),
],
),
),
Tab(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
2024-10-13 11:56:37 +00:00
const Icon(
Icons.shuffle_on_outlined,
size: 20,
),
2024-10-12 17:31:59 +00:00
const Gap(8),
Text('postListShuffle'.tr),
],
),
),
],
),
],
).paddingOnly(top: MediaQuery.of(context).padding.top),
),
),
2024-10-13 11:56:37 +00:00
expandedHeight: 104,
snap: true,
floating: true,
toolbarHeight: AppTheme.toolbarHeight(context),
leading: AppBarLeadingButton.adaptive(context),
actions: [
2024-10-13 15:12:23 +00:00
const BackgroundStateWidget(),
2024-10-13 13:48:53 +00:00
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
AppRouter.instance.pushNamed('postSearch');
},
),
const NotificationButton(),
SizedBox(
width: AppTheme.isLargeScreen(context) ? 8 : 16,
),
],
);
},
)
];
},
body: Obx(() {
if (_postController.isPreparing.isTrue) {
return const Center(
child: CircularProgressIndicator(),
2024-07-25 08:08:46 +00:00
);
}
return Column(
children: [
Expanded(
child: TabBarView(
physics: const NeverScrollableScrollPhysics(),
controller: _tabController,
children: [
RefreshIndicator(
onRefresh: () => _postController.reloadAllOver(),
child: CustomScrollView(slivers: [
2024-10-10 14:52:05 +00:00
ControlledPostListWidget(
2024-10-12 17:31:59 +00:00
padding: AppTheme.isLargeScreen(context)
? EdgeInsets.symmetric(
horizontal: 4,
vertical: 8,
)
: EdgeInsets.zero,
controller: _postController.pagingController,
onUpdate: () => _postController.reloadAllOver(),
),
]),
),
Obx(() {
if (auth.isAuthorized.value) {
return RefreshIndicator(
onRefresh: () => _postController.reloadAllOver(),
child: CustomScrollView(slivers: [
2024-10-10 14:52:05 +00:00
ControlledPostListWidget(
2024-10-12 17:31:59 +00:00
padding: AppTheme.isLargeScreen(context)
? EdgeInsets.symmetric(horizontal: 16)
: EdgeInsets.zero,
controller: _postController.pagingController,
onUpdate: () => _postController.reloadAllOver(),
),
]),
);
} else {
return SigninRequiredOverlay(
onDone: () => _postController.reloadAllOver(),
);
}
}),
PostShuffleSwiper(controller: _postController),
],
),
),
],
);
}),
2024-05-18 16:56:32 +00:00
),
2024-05-18 10:17:16 +00:00
),
);
}
2024-07-09 14:39:44 +00:00
@override
void dispose() {
2024-07-25 06:42:50 +00:00
_postController.dispose();
if (_subscriptions != null) {
for (final subscription in _subscriptions!) {
subscription.cancel();
}
}
2024-07-09 14:39:44 +00:00
super.dispose();
}
2024-05-18 10:17:16 +00:00
}