following_page.dart 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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: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_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.read(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: UniqueKey(),
  63. onLoad: () async{
  64. Log.d("--following--onLoad");
  65. vm.loadMore();
  66. },
  67. onRefresh: () async{
  68. Log.d("--following--onRefresh");
  69. vm.onRefresh();
  70. },
  71. child: LoadStateLayout(
  72. state: state.loadingState,
  73. errorMessage: state.errorMessage,
  74. errorRetry: () {
  75. vm.retryRequest();
  76. },
  77. successSliverWidget: [
  78. SliverList(
  79. delegate: SliverChildBuilderDelegate(
  80. (context, index){
  81. return _buildNewsItem(context, ref, state.list![index], vm, index);
  82. },
  83. childCount: state.list!.length
  84. ),
  85. )
  86. ],
  87. ),
  88. ),
  89. )
  90. ],
  91. )
  92. );
  93. }
  94. Widget _buildNewsItem(BuildContext context, WidgetRef ref, Map<String, dynamic> item, vm, int itemIdx){
  95. String card_title = item.getValue("title", "");
  96. String card_created_at = item.getValue("created_at", "");
  97. Map<String, dynamic>? card_account = item.getValue<Map<String,dynamic>>("account", {});
  98. String card_avator = card_account?['avator']?? "";
  99. bool card_followed = card_account?['followed']??false;
  100. String card_content = item.getValue("content", "");
  101. List? card_resources = item.getValue<List>("resources", [])?? [];
  102. bool card_liked = item.getValue("liked", false);
  103. int card_likes_count = item.getValue("likes_count", 0);
  104. int card_comments_count = item.getValue("comments_count", 0);
  105. return Container(
  106. margin: const EdgeInsets.only(left: 15, right: 15,top: 14,bottom: 14),
  107. padding: const EdgeInsets.only(left: 15, right: 15,top: 17,bottom: 0),
  108. decoration: BoxDecoration(
  109. color: context.appColors.textWhite,
  110. borderRadius: BorderRadius.circular(10),
  111. boxShadow: [
  112. BoxShadow(
  113. color: ColorUtils.string2Color("#E4E7EB").withOpacity(0.5),
  114. spreadRadius: 0,
  115. blurRadius: 4,
  116. offset: const Offset(0, 4), // changes position of shadow
  117. ),
  118. ]
  119. ),
  120. child: Stack(
  121. children: [
  122. Column(
  123. mainAxisAlignment: MainAxisAlignment.center,
  124. crossAxisAlignment: CrossAxisAlignment.center,
  125. children: [
  126. // 卡片头部(头像 标题 时间)
  127. Container(
  128. child: NewsFeedCardHeader(
  129. key: UniqueKey(),
  130. title: card_title,
  131. avator: card_avator,
  132. time: card_created_at,
  133. ),
  134. ),
  135. const SizedBox(height: 15),
  136. // 卡片中间 (文字和图片)
  137. NewsFeedCardContent(
  138. key: UniqueKey(),
  139. content: card_content,
  140. imageUrls: card_resources,
  141. ),
  142. const SizedBox(height: 16),
  143. // // 卡片底部 (点赞 评论 分享)
  144. Container(
  145. padding: const EdgeInsets.only(top: 10, bottom: 15),
  146. decoration: BoxDecoration(
  147. // color: Colors.white,
  148. border: Border(
  149. top: BorderSide(color: context.appColors.dividerDefault, width: 0.5),
  150. )
  151. ),
  152. child: NewsFeedCardFooter(
  153. key: UniqueKey(),
  154. isLike: card_liked,
  155. likes_count: card_likes_count,
  156. comments_count: card_comments_count,
  157. onLike: (){
  158. vm.handlerClickActionBtn('like', item, itemIdx);
  159. },
  160. onComment: (){
  161. vm.handlerClickActionBtn('comments', item, itemIdx);
  162. },
  163. onShare: (){
  164. vm.handlerClickActionBtn('share', item, itemIdx);
  165. },
  166. ),
  167. ),
  168. ]
  169. ),
  170. // 右上角 关注/取消关注 按钮
  171. Visibility(
  172. visible: card_followed ? false: true,
  173. child: Positioned(
  174. right: 10,
  175. top: -5,
  176. child: Container(
  177. width: 83.5,
  178. height: 45.5,
  179. alignment: Alignment.center,
  180. // decoration: BoxDecoration(
  181. // color: ColorUtils.string2Color('#4161D0'),
  182. // borderRadius: BorderRadius.circular(5),
  183. // ),
  184. child: MyButton(
  185. text: '+Follow',
  186. textColor: Colors.white,
  187. backgroundColor: ColorUtils.string2Color('#4161D0'),
  188. radius: 8,
  189. minHeight: 27.5,
  190. padding: const EdgeInsets.only(left: 5, right: 5,top:9,bottom:9),
  191. fontWeight: FontWeight.w400,
  192. fontSize: 14,
  193. onPressed: (){
  194. vm.handlerFollow(context, card_followed);
  195. },
  196. ),
  197. )
  198. ),
  199. )
  200. ],
  201. ),
  202. ).constrained(
  203. width: double.infinity,
  204. // height: 580,
  205. // minHeight: 300,
  206. ).onTap((){
  207. vm.handlerGotoDetail(context, item['id']);
  208. });
  209. }
  210. }