news_page.dart 9.1 KB

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