newsfeed_page.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import 'package:cpt_community/components/newsfeed_card_content.dart';
  2. import 'package:cpt_community/components/newsfeed_card_footer.dart';
  3. import 'package:cs_resources/generated/assets.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:auto_route/auto_route.dart';
  6. import 'package:flutter/rendering.dart';
  7. import 'package:hooks_riverpod/hooks_riverpod.dart';
  8. import 'package:router/ext/auto_router_extensions.dart';
  9. import 'package:shared/utils/color_utils.dart';
  10. import 'package:shared/utils/log_utils.dart';
  11. import 'package:widgets/ext/ex_widget.dart';
  12. import 'package:widgets/my_load_image.dart';
  13. import 'package:widgets/my_text_view.dart';
  14. import 'package:widgets/widget_export.dart';
  15. import '../../components/newfeed_card_header.dart';
  16. import '../../router/page/community_page_router.dart';
  17. import '../community/community_vm.dart';
  18. import 'newsfeed_tabs.dart';
  19. import 'newsfeed_vm.dart';
  20. @RoutePage()
  21. class NewsfeedPage extends HookConsumerWidget {
  22. const NewsfeedPage({Key? key}) : super(key: key);
  23. //启动当前页面
  24. static void startInstance({BuildContext? context}) {
  25. if (context != null) {
  26. context.router.push(const NewsfeedPageRoute());
  27. } else {
  28. appRouter.push(const NewsfeedPageRoute());
  29. }
  30. }
  31. Widget _buildTabsSection(BuildContext context, WidgetRef ref, vm){
  32. return Container(
  33. width: double.infinity,
  34. padding: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  35. child: NewsfeedTabs(
  36. tabsList: vm.state.tabsList,
  37. ),
  38. );
  39. }
  40. Widget _buildPostSection(BuildContext context, WidgetRef ref, vm){
  41. return Container(
  42. height: 65.5,
  43. width: double.infinity,
  44. padding: const EdgeInsets.only(left: 20, right: 20),
  45. color: Colors.white,
  46. child: Row(
  47. children: [
  48. const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
  49. Expanded(
  50. child: Row(
  51. children: [
  52. Expanded(
  53. child: Container(
  54. // height: 65.5,
  55. // color: Colors.blue,
  56. child: MyTextView(
  57. "What’s on your mind?",
  58. textColor: ColorUtils.string2Color('#000000'),
  59. fontSize: 15,
  60. marginLeft: 15,
  61. alignment: Alignment.centerLeft,
  62. textAlign: TextAlign.left,
  63. backgroundColor: ColorUtils.string2Color('#ffffff'),
  64. maxLines: 1,
  65. isFontMedium: true,
  66. ),
  67. ),
  68. ),
  69. const MyAssetImage(
  70. Assets.communityCamera,
  71. width: 21,
  72. height: 16.5,
  73. ),
  74. ],
  75. ).onTap((){
  76. vm.handlerGotoPost(context);
  77. }),
  78. ),
  79. ],
  80. ),
  81. );
  82. }
  83. Widget _buildNewsItem(BuildContext context, WidgetRef ref, item, vm){
  84. return Container(
  85. width: double.infinity,
  86. // padding: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  87. color: Colors.yellow,
  88. child: Container(
  89. margin: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  90. color: Colors.white,
  91. padding: const EdgeInsets.only(left: 15, right: 15,top: 17,bottom: 17),
  92. height: 280,
  93. child: Column(
  94. mainAxisAlignment: MainAxisAlignment.center,
  95. crossAxisAlignment: CrossAxisAlignment.start,
  96. children: [
  97. // 卡片头部(头像 标题 时间)
  98. NewsFeedCardHeader(
  99. title: item['title'],
  100. avator: item['avator'],
  101. time: item['time'],
  102. ),
  103. const SizedBox(height: 15),
  104. // 卡片中间 (文字和图片)
  105. Expanded(
  106. child: NewsFeedCardContent(
  107. content: item['content'],
  108. imageUrls: item['imageUrls'],
  109. ),
  110. ),
  111. const SizedBox(height: 26),
  112. // // 卡片底部 (点赞 评论 分享)
  113. NewsFeedCardFooter(
  114. isLike: item['isLike'],
  115. ),
  116. ]
  117. ),
  118. )
  119. );
  120. }
  121. Widget _buildNesFeedList(BuildContext context, WidgetRef ref, vm){
  122. // return Container(
  123. // height: 100,
  124. // color: Colors.blue,
  125. // );
  126. // List items = List.generate(20, (index) => "Item $index");
  127. // List items = _vm.state.list.fromMap();
  128. final itemList = vm.state.list?? [];
  129. if(itemList.isEmpty){
  130. return const Center(child: Text('No Data'));
  131. }else {
  132. List itemsList = vm.state.list.toList();
  133. return ListView.builder(
  134. itemCount: itemsList.length,
  135. itemBuilder: (context, index) {
  136. return _buildNewsItem(context, ref, itemsList[index], vm);
  137. },
  138. );
  139. }
  140. }
  141. @override
  142. Widget build(BuildContext context, WidgetRef ref) {
  143. final vm = ref.read(newsfeedVmProvider.notifier);
  144. return Scaffold(
  145. backgroundColor: ColorUtils.string2Color("#F2F3F6"),
  146. body: Column(
  147. children: [
  148. _buildTabsSection(context, ref, vm),
  149. _buildPostSection(context, ref, vm),
  150. // Expanded(
  151. // child: EasyRefresh(
  152. // // 上拉加载
  153. // onLoad: () async{
  154. // Log.d("----onLoad");
  155. // vm.onLoadData();
  156. // },
  157. // // 下拉刷新
  158. // onRefresh: () async{
  159. // Log.d("----onRefresh");
  160. // vm.refreshListData();
  161. // },
  162. // child: _buildNesFeedList(context, ref, vm),
  163. // ),
  164. // )
  165. NotificationListener <ScrollNotification>(
  166. onNotification: (ScrollNotification notification) {
  167. // 检查当前页面是否是可见的
  168. bool isDownOrUp = notification.metrics.axis == Axis.vertical;
  169. if (notification is UserScrollNotification) {
  170. // 检查滚动方向
  171. switch (notification.direction) {
  172. case ScrollDirection.forward:
  173. print('Scrolling down');
  174. break;
  175. case ScrollDirection.reverse:
  176. print('Scrolling up');
  177. break;
  178. case ScrollDirection.idle:
  179. print('Scrolling stopped');
  180. break;
  181. }
  182. } else if (notification is ScrollUpdateNotification) {
  183. // 检查滚动位置变化
  184. double currentScrollPosition = notification.metrics.pixels;
  185. double maxScrollExtent = notification.metrics.maxScrollExtent;
  186. // 判断是否满足某个条件
  187. if (currentScrollPosition > 0 && currentScrollPosition < maxScrollExtent) {
  188. print('Current scroll position: $currentScrollPosition');
  189. // 在这里添加你的条件判断逻辑
  190. }
  191. // 只有当上下滚动时才拦截通知
  192. if (notification.metrics.axis == Axis.vertical) {
  193. final tabsRouter = ref.watch(communityVmProvider).tabsRouter;
  194. final curUseTag = ref.watch(communityVmProvider).useTag;
  195. if(curUseTag != 0 ){
  196. // 非当前 页面都阻止滚动
  197. return true; // 返回 true 表示已处理通知
  198. }
  199. }
  200. }
  201. return false; // 返回 false 表示不拦截通知
  202. },
  203. child: Expanded(
  204. child: _buildNesFeedList(context, ref, vm),
  205. ),
  206. )
  207. ],
  208. ),
  209. );
  210. }
  211. }