following_page.dart 8.7 KB

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