following_page.dart 7.7 KB

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