news_page.dart 9.0 KB

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