following_page.dart 7.8 KB

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