following_page.dart 8.8 KB

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