following_page.dart 7.7 KB

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