news_page.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:auto_route/auto_route.dart';
  5. import 'package:flutter/rendering.dart';
  6. import 'package:flutter_hooks/flutter_hooks.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/ext_dart.dart';
  11. import 'package:shared/utils/log_utils.dart';
  12. import 'package:widgets/load_state_layout.dart';
  13. import 'package:widgets/my_button.dart';
  14. import 'package:widgets/my_load_image.dart';
  15. import 'package:widgets/ext/ex_widget.dart';
  16. import 'package:widgets/my_text_view.dart';
  17. import 'package:widgets/my_appbar.dart';
  18. import 'package:cs_resources/theme/app_colors_theme.dart';
  19. import 'package:widgets/widget_export.dart';
  20. import 'package:easy_refresh/easy_refresh.dart';
  21. import '../../../components/newfeed_card_header.dart';
  22. import '../../../components/newsfeed_card_content.dart';
  23. import '../../../components/newsfeed_card_footer.dart';
  24. import '../../../router/page/community_page_router.dart';
  25. import '../community_vm.dart';
  26. import 'news_vm.dart';
  27. @RoutePage()
  28. class NewsPage extends HookConsumerWidget {
  29. const NewsPage({Key? key}) : super(key: key);
  30. //启动当前页面
  31. static void startInstance({BuildContext? context}) {
  32. if (context != null) {
  33. context.router.push(const NewsPageRoute());
  34. } else {
  35. appRouter.push(const NewsPageRoute());
  36. }
  37. }
  38. @override
  39. Widget build(BuildContext context, WidgetRef ref) {
  40. final vm = ref.read(newsVmProvider.notifier);
  41. final state = ref.watch(newsVmProvider);
  42. useEffect((){
  43. // 组件挂载时执行 - 执行接口请求
  44. Future.microtask(() => vm.initPageData());
  45. return () {
  46. // 组件卸载时执行
  47. };
  48. }, []);
  49. return Scaffold(
  50. // appBar: MyAppBar.appBar(
  51. // context,
  52. // "news",
  53. // backgroundColor: context.appColors.whiteBG,
  54. // ),
  55. backgroundColor: ColorUtils.string2Color("#F2F3F6"),
  56. body: Column(
  57. mainAxisSize: MainAxisSize.max,
  58. children: [
  59. Expanded(
  60. child: EasyRefresh(
  61. key: const PageStorageKey<String>('news'),
  62. controller: vm.refreshController,
  63. onLoad: () async{
  64. Log.d("--news--onLoad");
  65. vm.loadMore();
  66. },
  67. onRefresh: () async{
  68. Log.d("--news--onRefresh");
  69. vm.onRefresh();
  70. },
  71. child: LoadStateLayout(
  72. state: state.loadingState,
  73. errorMessage: state.errorMessage,
  74. errorRetry: () {
  75. vm.retryRequest();
  76. },
  77. // sliverScrollController: vm.scrollController,
  78. successSliverWidget: [
  79. SliverList(
  80. delegate: SliverChildBuilderDelegate(
  81. (context, index){
  82. return _buildNewsItem(context, ref, state.list![index], vm, index);
  83. },
  84. childCount: state.list!.length
  85. ),
  86. )
  87. ],
  88. ),
  89. ),
  90. )
  91. ],
  92. )
  93. );
  94. }
  95. Widget _buildNewsItem(BuildContext context, WidgetRef ref, Map<String, dynamic> item, vm, int itemIdx){
  96. String card_title = item.getValue("title", "");
  97. int card_id = item.getValue("id", null);
  98. String card_created_at = item.getValue("created_at", "");
  99. Map<String, dynamic>? card_account = item.getValue<Map<String,dynamic>>("account", null);
  100. String card_avatar = card_account?['avatar']?? "";
  101. String card_count_name = card_account?['name']?? "";
  102. bool card_followed = card_account?['followed']??false;
  103. int card_count_id = card_account?['id']?? null;
  104. String card_content = item.getValue("content", "");
  105. List? card_resources = item.getValue<List>("resources", [])?? [];
  106. bool card_liked = item.getValue("liked", false);
  107. int card_likes_count = item.getValue("likes_count", 0);
  108. int card_comments_count = item.getValue("comments_count", 0);
  109. return Container(
  110. margin: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  111. padding: const EdgeInsets.only(left: 15, right: 15,top: 17,bottom: 0),
  112. decoration: BoxDecoration(
  113. color: context.appColors.textWhite,
  114. borderRadius: BorderRadius.circular(10),
  115. boxShadow: [
  116. BoxShadow(
  117. color: ColorUtils.string2Color("#E4E7EB").withOpacity(0.5),
  118. spreadRadius: 0,
  119. blurRadius: 4,
  120. offset: const Offset(0, 4), // changes position of shadow
  121. ),
  122. ]
  123. ),
  124. child: Stack(
  125. children: [
  126. Column(
  127. mainAxisAlignment: MainAxisAlignment.center,
  128. crossAxisAlignment: CrossAxisAlignment.center,
  129. children: [
  130. // 卡片头部(头像 标题 时间)
  131. Container(
  132. child: NewsFeedCardHeader(
  133. key: UniqueKey(),
  134. title: card_count_name,
  135. avator: card_avatar,
  136. time: card_created_at,
  137. ),
  138. ),
  139. const SizedBox(height: 15),
  140. // 卡片中间 (文字和图片)
  141. NewsFeedCardContent(
  142. key: UniqueKey(),
  143. content: card_content,
  144. imageUrls: card_resources,
  145. ),
  146. const SizedBox(height: 16),
  147. // // 卡片底部 (点赞 评论 分享)
  148. Container(
  149. padding: const EdgeInsets.only(top: 10, bottom: 15),
  150. decoration: BoxDecoration(
  151. // color: Colors.white,
  152. border: Border(
  153. top: BorderSide(color: context.appColors.dividerDefault, width: 0.5),
  154. )
  155. ),
  156. child: NewsFeedCardFooter(
  157. key: UniqueKey(),
  158. isLike: card_liked,
  159. likes_count: card_likes_count,
  160. comments_count: card_comments_count,
  161. onLike: (){
  162. vm.handlerClickActionBtn('like', item, itemIdx);
  163. },
  164. onComment: (){
  165. vm.handlerClickActionBtn('comments', item, itemIdx);
  166. },
  167. onShare: (){
  168. vm.handlerClickActionBtn('share', item, itemIdx);
  169. },
  170. ),
  171. ),
  172. ]
  173. ),
  174. // 右上角 关注/取消关注 按钮
  175. Positioned(
  176. right: 10,
  177. top: -5,
  178. child: Container(
  179. width: 83.5,
  180. height: 50.5,
  181. alignment: Alignment.center,
  182. // decoration: BoxDecoration(
  183. // color: ColorUtils.string2Color('#4161D0'),
  184. // borderRadius: BorderRadius.circular(5),
  185. // ),
  186. child: HookBuilder(
  187. builder: (context) {
  188. final isFollowedState = useState<bool>(card_followed);
  189. return MyButton(
  190. text: isFollowedState.value ? 'Followed': '+Follow',
  191. textColor: isFollowedState.value ? context.appColors.disEnableGray: context.appColors.textWhite,
  192. disabledTextColor: context.appColors.disEnableGray,
  193. backgroundColor: isFollowedState.value ? Colors.transparent : ColorUtils.string2Color('#4161D0'),
  194. side: BorderSide(color: !isFollowedState.value ? Colors.transparent : context.appColors.disEnableGray, width: 0.5),
  195. radius: 8,
  196. minHeight: 27.5,
  197. padding: const EdgeInsets.only(left: 5, right: 5,top:9,bottom:9),
  198. fontWeight: FontWeight.w400,
  199. fontSize: 14,
  200. onPressed: () async{
  201. bool asyncResult = await vm.handlerFollow(context,card_count_id, card_id, isFollowedState.value );
  202. if(asyncResult){
  203. // 成功 关注->取消关注 取消关注->关注
  204. isFollowedState.value = !isFollowedState.value;
  205. }
  206. },
  207. );
  208. }
  209. ),
  210. )
  211. )
  212. ],
  213. ),
  214. ).constrained(
  215. width: double.infinity,
  216. // height: 580,
  217. // minHeight: 300,
  218. ).onTap((){
  219. vm.handlerGotoDetail(context, item['id']);
  220. });
  221. }
  222. }