news_page.dart 7.0 KB

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