123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- import 'package:cs_resources/generated/assets.dart';
- import 'package:flutter/material.dart';
- import 'package:auto_route/auto_route.dart';
- import 'package:flutter/rendering.dart';
- import 'package:flutter_hooks/flutter_hooks.dart';
- import 'package:hooks_riverpod/hooks_riverpod.dart';
- import 'package:router/ext/auto_router_extensions.dart';
- import 'package:shared/utils/color_utils.dart';
- import 'package:shared/utils/log_utils.dart';
- import 'package:widgets/my_load_image.dart';
- import 'package:widgets/ext/ex_widget.dart';
- import 'package:widgets/my_text_view.dart';
- import 'package:widgets/my_appbar.dart';
- import 'package:cs_resources/theme/app_colors_theme.dart';
- import 'package:widgets/widget_export.dart';
- import '../../router/page/community_page_router.dart';
- import 'newsfeed_tabs.dart';
- import 'community_vm.dart';
- import 'customSilverHeaderTabs.dart';
- @RoutePage()
- class CommunityPage extends HookConsumerWidget {
- const CommunityPage({Key? key}) : super(key: key);
- //启动当前页面
- static void startInstance({BuildContext? context}) {
- if (context != null) {
- context.router.push(const CommunityPageRoute());
- } else {
- appRouter.push(const CommunityPageRoute());
- }
- }
- bool _isPreventScroll(ScrollNotification notification, WidgetRef ref){
- // 检查当前页面是否是可见的
- bool isDownOrUp = notification.metrics.axis == Axis.vertical;
- if (notification is UserScrollNotification) {
- // 检查滚动方向
- switch (notification.direction) {
- case ScrollDirection.forward:
- print('Scrolling down');
- break;
- case ScrollDirection.reverse:
- print('Scrolling up');
- break;
- case ScrollDirection.idle:
- print('Scrolling stopped');
- break;
- }
- } else if (notification is ScrollUpdateNotification) {
- // 检查滚动位置变化
- double currentScrollPosition = notification.metrics.pixels;
- double maxScrollExtent = notification.metrics.maxScrollExtent;
- // 判断是否满足某个条件
- if (currentScrollPosition > 0 && currentScrollPosition < maxScrollExtent) {
- print('Current scroll position: $currentScrollPosition');
- // 在这里添加你的条件判断逻辑
- }
- // 只有当上下滚动时才拦截通知
- if (isDownOrUp) {
- final vm = ref.read(communityVmProvider.notifier);
- final currentTabRouter = vm.state!.tabsRouter;
- if(currentTabRouter!.activeIndex == 0){
- return false; // false 不拦截
- }else {
- return true; // true 拦截
- }
- }
- }
- return false;
- }
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final vm = ref.read(communityVmProvider.notifier);
- BuildContext contextPage = context;
- // 创建 GlobalKey
- final _tabsRouterKey = GlobalKey<AutoTabsRouterState>();
- // useEffect((){
- // Log.d("CommunityPage initState");
- // return () {
- // Log.d("CommunityPage dispose");
- // };
- // },[]);
- return Scaffold(
- appBar: MyAppBar.appBar(
- context,
- "Community",
- backgroundColor: context.appColors.whiteBG,
- ),
- backgroundColor: context.appColors.backgroundDefault,
- body: NestedScrollView(
- headerSliverBuilder: (context, innerBoxIsScrolled) {
- return [
- SliverToBoxAdapter(
- child: Consumer(
- builder: (context, ref, _) {
- return _buildTopSection(context, ref, vm);
- },
- ),
- ),
- // SliverPersistentHeader(
- // pinned: true, // 吸顶
- // delegate: CustomSliverPersistentHeaderDelegate(
- // child: _buildTabsSection(contextPage, ref),
- // ),
- // ),
- ];
- },
- body: NotificationListener<ScrollNotification>(
- onNotification: (ScrollNotification notification) {
- // 是否拦截滚动 false 表示不拦截通知
- // return _isPreventScroll(notification, ref);
- return false;
- },
- child: AutoTabsRouter.pageView(
- key: _tabsRouterKey,
- routes: const [
- NewsPageRoute(),
- FollowingPageRoute(),
- ForyouPageRoute()
- ],
- builder: (context, child, pageController) {
- final tabsRouter = AutoTabsRouter.of(context);
- // Log.d("autotabsrouter build ${tabsRouter.activeIndex}");
- // Log.d("communityVmProvider.notifier state.tabsRouter.tabsRouter: ${ref.read(communityVmProvider.notifier).state.tabsRouter}");
- // ref.read(communityVmProvider.notifier).state.tabsRouter = tabsRouter;
- // ref.read(communityVmProvider.notifier).state.pageController = pageController;
- // Log.d("communityVmProvider.notifier state.tabsRouter.tabsRouter.activeIndex: ${ref.read(communityVmProvider.notifier).state.tabsRouter.activeIndex}");
- WidgetsBinding.instance.addPostFrameCallback((_) {
- vm.setTabsRouterAndPageController(tabsRouter, pageController);
- });
- return Column(
- mainAxisSize: MainAxisSize.max,
- children: [
- // tab 组件
- _buildTabsSection(contextPage, ref, tabsRouter),
- // post 组件
- _buildPostSection(context, ref, vm),
- Expanded(
- child: child,
- ),
- ],
- );
- },
- ),
- ),
- )
- );
- }
- Widget _buildTopSection(BuildContext context, WidgetRef ref, vm) {
- final topSectionsData = vm.topSectionsData;
- int curTagIdx = 0;
- return Container(
- color: context.appColors.whiteBG,
- padding: const EdgeInsets.only(top: 23, bottom: 30),
- child: Center(
- child: Row(
- mainAxisSize: MainAxisSize.max,
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: List.generate(topSectionsData.length, (index) {
- final item = topSectionsData[index];
- return Column(
- children: [
- Container(
- width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
- height: 70,
- decoration: BoxDecoration(
- // color: currentTabIdx == index ? ColorUtils.string2Color('#E6F2FF') : Colors.white,
- shape: BoxShape.circle, // 设置为圆形
- boxShadow: index == curTagIdx ? [
- BoxShadow(
- color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
- blurRadius: 5, // 设置模糊半径
- spreadRadius: 0.05, // 控制阴影扩散
- offset: const Offset(0, 4), // 设置阴影偏移量
- ), ] : [],// 未选中时无阴影,
- ),
- child: MyAssetImage(
- item['icon'],
- width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
- height: 70,
- ).onTap(() {
- vm.handlerChangeCommunityType(context,index);
- },
- type: ClickType.throttle,
- ),
- ),
- SizedBox.fromSize(size: const Size(0, 9)),
- MyTextView(
- item['title'],
- fontSize: 15,
- textColor: index == curTagIdx ? ColorUtils.string2Color('#4161D0'):Colors.black,
- textAlign: TextAlign.center,
- isFontMedium: true,
- ),
- ],
- ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
- }),
- ),
- ),
- );
- }
- Widget _buildTabsSection(BuildContext context, WidgetRef ref, tabsRouter){
- final vm = ref.read(communityVmProvider.notifier);
- final tabsList = ref.watch(communityVmProvider.select((state) => state.tabsList));
- return Container(
- width: double.infinity,
- padding: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
- child: NewsfeedTabs(
- key: UniqueKey(),
- tabsList: tabsList!,
- tabsRouter: tabsRouter,
- onClickAction:(activeTabIdx){
- vm.handlerChangeTab(activeTabIdx, tabsRouter);
- }
- ),
- );
- }
- Widget _buildPostSection(BuildContext context, WidgetRef ref, vm){
- return Container(
- height: 65.5,
- width: double.infinity,
- padding: const EdgeInsets.only(left: 20, right: 20),
- color: Colors.white,
- child: Row(
- children: [
- const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
- Expanded(
- child: MyTextView(
- "What’s on your mind?",
- textColor: ColorUtils.string2Color('#000000'),
- fontSize: 15,
- marginLeft: 15,
- alignment: Alignment.centerLeft,
- textAlign: TextAlign.left,
- backgroundColor: ColorUtils.string2Color('#ffffff'),
- maxLines: 1,
- isFontMedium: true,
- ),
- ),
- const MyAssetImage(
- Assets.communityCamera,
- width: 21,
- height: 16.5,
- ),
- ],
- ).onTap((){
- vm.handlerGotoPost(context);
- }),
- );
- }
- }
|