newsfeed_page.dart 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 'package:widgets/my_button.dart';
  16. import '../../components/newfeed_card_header.dart';
  17. import '../../router/page/community_page_router.dart';
  18. import '../community/community_vm.dart';
  19. import 'newsfeed_tabs.dart';
  20. import 'newsfeed_vm.dart';
  21. @RoutePage()
  22. class NewsfeedPage extends HookConsumerWidget {
  23. const NewsfeedPage({Key? key}) : super(key: key);
  24. //启动当前页面
  25. static void startInstance({BuildContext? context}) {
  26. if (context != null) {
  27. context.router.push(const NewsfeedPageRoute());
  28. } else {
  29. appRouter.push(const NewsfeedPageRoute());
  30. }
  31. }
  32. Widget _buildTabsSection(BuildContext context, WidgetRef ref, vm){
  33. return Container(
  34. width: double.infinity,
  35. padding: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  36. child: NewsfeedTabs(
  37. key: UniqueKey(),
  38. tabsList: vm.state.tabsList,
  39. ),
  40. );
  41. }
  42. Widget _buildPostSection(BuildContext context, WidgetRef ref, vm){
  43. return Container(
  44. height: 65.5,
  45. width: double.infinity,
  46. padding: const EdgeInsets.only(left: 20, right: 20),
  47. color: Colors.white,
  48. child: Row(
  49. children: [
  50. const MyAssetImage(Assets.communityNesFeed, width: 45,height: 45,),
  51. Expanded(
  52. child: Row(
  53. children: [
  54. Expanded(
  55. child: Container(
  56. // height: 65.5,
  57. // color: Colors.blue,
  58. child: MyTextView(
  59. "What’s on your mind?",
  60. textColor: ColorUtils.string2Color('#000000'),
  61. fontSize: 15,
  62. marginLeft: 15,
  63. alignment: Alignment.centerLeft,
  64. textAlign: TextAlign.left,
  65. backgroundColor: ColorUtils.string2Color('#ffffff'),
  66. maxLines: 1,
  67. isFontMedium: true,
  68. ),
  69. ),
  70. ),
  71. const MyAssetImage(
  72. Assets.communityCamera,
  73. width: 21,
  74. height: 16.5,
  75. ),
  76. ],
  77. ).onTap((){
  78. vm.handlerGotoPost(context);
  79. }),
  80. ),
  81. ],
  82. ),
  83. );
  84. }
  85. Widget _buildNewsItem(BuildContext context, WidgetRef ref, item, vm){
  86. return Container(
  87. width: double.infinity,
  88. // color: Colors.yellow,
  89. child: Stack(
  90. children: [
  91. Container(
  92. margin: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  93. color: Colors.white,
  94. padding: const EdgeInsets.only(left: 15, right: 15,top: 17,bottom: 17),
  95. height: 280,
  96. child: Column(
  97. mainAxisAlignment: MainAxisAlignment.center,
  98. crossAxisAlignment: CrossAxisAlignment.start,
  99. children: [
  100. // 卡片头部(头像 标题 时间)
  101. NewsFeedCardHeader(
  102. key: UniqueKey(),
  103. title: item['title'],
  104. avator: item['avator'],
  105. time: item['time'],
  106. ),
  107. const SizedBox(height: 15),
  108. // 卡片中间 (文字和图片)
  109. Expanded(
  110. child: NewsFeedCardContent(
  111. key: UniqueKey(),
  112. content: item['content'],
  113. imageUrls: item['imageUrls'],
  114. ),
  115. ),
  116. const SizedBox(height: 26),
  117. // // 卡片底部 (点赞 评论 分享)
  118. NewsFeedCardFooter(
  119. key: UniqueKey(),
  120. isLike: item['isLike'],
  121. onLike: (){
  122. vm.handlerClickActionBtn('like', item);
  123. },
  124. onComment: (){
  125. vm.handlerClickActionBtn('comments', item);
  126. },
  127. onShare: (){
  128. vm.handlerClickActionBtn('share', item);
  129. },
  130. ),
  131. ]
  132. ),
  133. ),
  134. // 右上角 关注/取消关注 按钮
  135. Visibility(
  136. visible: !item['isFollow'],
  137. child: Positioned(
  138. right: 40,
  139. top: 35,
  140. child: Container(
  141. width: 83.5,
  142. height: 45.5,
  143. alignment: Alignment.center,
  144. // decoration: BoxDecoration(
  145. // color: ColorUtils.string2Color('#4161D0'),
  146. // borderRadius: BorderRadius.circular(5),
  147. // ),
  148. child: MyButton(
  149. text: '+Follow',
  150. textColor: Colors.white,
  151. backgroundColor: ColorUtils.string2Color('#4161D0'),
  152. radius: 8,
  153. minHeight: 27.5,
  154. padding: const EdgeInsets.only(left: 5, right: 5,top:9,bottom:9),
  155. fontWeight: FontWeight.w400,
  156. fontSize: 14,
  157. onPressed: (){
  158. // Navigator.pop(context);
  159. },
  160. ),
  161. )
  162. ),
  163. )
  164. ],
  165. ),
  166. );
  167. }
  168. Widget _buildNesFeedList(BuildContext context, WidgetRef ref, vm){
  169. final itemList = vm.state.list?? [];
  170. if(itemList.isEmpty){
  171. return const Center(child: Text('No Data'));
  172. }else {
  173. List itemsList = vm.state.list.toList();
  174. return ListView.builder(
  175. key: UniqueKey(),
  176. itemCount: itemsList.length,
  177. itemBuilder: (context, index) {
  178. return _buildNewsItem(context, ref, itemsList[index], vm);
  179. },
  180. );
  181. }
  182. }
  183. @override
  184. Widget build(BuildContext context, WidgetRef ref) {
  185. final vm = ref.read(newsfeedVmProvider.notifier);
  186. return Scaffold(
  187. backgroundColor: ColorUtils.string2Color("#F2F3F6"),
  188. body: Column(
  189. children: [
  190. // 顶部的 tab
  191. _buildTabsSection(context, ref, vm),
  192. // post 组件
  193. _buildPostSection(context, ref, vm),
  194. // Expanded(
  195. // child: EasyRefresh(
  196. // // 上拉加载
  197. // onLoad: () async{
  198. // Log.d("----onLoad");
  199. // vm.onLoadData();
  200. // },
  201. // // 下拉刷新
  202. // onRefresh: () async{
  203. // Log.d("----onRefresh");
  204. // vm.refreshListData();
  205. // },
  206. // child: _buildNesFeedList(context, ref, vm),
  207. // ),
  208. // )
  209. NotificationListener <ScrollNotification>(
  210. onNotification: (ScrollNotification notification) {
  211. // // 检查当前页面是否是可见的
  212. // bool isDownOrUp = notification.metrics.axis == Axis.vertical;
  213. // if (notification is UserScrollNotification) {
  214. // // 检查滚动方向
  215. // switch (notification.direction) {
  216. // case ScrollDirection.forward:
  217. // print('Scrolling down');
  218. // break;
  219. // case ScrollDirection.reverse:
  220. // print('Scrolling up');
  221. // break;
  222. // case ScrollDirection.idle:
  223. // print('Scrolling stopped');
  224. // break;
  225. // }
  226. // } else if (notification is ScrollUpdateNotification) {
  227. // // 检查滚动位置变化
  228. // double currentScrollPosition = notification.metrics.pixels;
  229. // double maxScrollExtent = notification.metrics.maxScrollExtent;
  230. //
  231. // // 判断是否满足某个条件
  232. // if (currentScrollPosition > 0 && currentScrollPosition < maxScrollExtent) {
  233. // print('Current scroll position: $currentScrollPosition');
  234. // // 在这里添加你的条件判断逻辑
  235. // }
  236. //
  237. // // 只有当上下滚动时才拦截通知
  238. // if (notification.metrics.axis == Axis.vertical) {
  239. // final tabsRouter = ref.watch(communityVmProvider).tabsRouter;
  240. // final curUseTag = ref.watch(communityVmProvider).useTag;
  241. // if(curUseTag != 0 ){
  242. // // 非当前 页面都阻止滚动
  243. // return true; // 返回 true 表示已处理通知
  244. // }
  245. // }
  246. // }
  247. return false; // 返回 false 表示不拦截通知
  248. },
  249. child: Expanded(
  250. child: _buildNesFeedList(context, ref, vm),
  251. ),
  252. )
  253. ],
  254. ),
  255. );
  256. }
  257. }