news_page.dart 7.8 KB

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