news_page.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // height: 280,
  96. decoration: BoxDecoration(
  97. color: context.appColors.textWhite,
  98. borderRadius: BorderRadius.circular(10),
  99. boxShadow: [
  100. BoxShadow(
  101. color: ColorUtils.string2Color("#E4E7EB").withOpacity(0.5),
  102. spreadRadius: 0,
  103. blurRadius: 4,
  104. offset: const Offset(0, 4), // changes position of shadow
  105. ),
  106. ]
  107. ),
  108. child: Stack(
  109. children: [
  110. Column(
  111. mainAxisAlignment: MainAxisAlignment.center,
  112. crossAxisAlignment: CrossAxisAlignment.center,
  113. children: [
  114. // 卡片头部(头像 标题 时间)
  115. Container(
  116. child: NewsFeedCardHeader(
  117. key: UniqueKey(),
  118. title: item['title'],
  119. avator: item['avator'],
  120. time: item['time'],
  121. ),
  122. ),
  123. const SizedBox(height: 15),
  124. // 卡片中间 (文字和图片)
  125. NewsFeedCardContent(
  126. key: UniqueKey(),
  127. content: item['content'],
  128. imageUrls: item['imageUrls'],
  129. ),
  130. const SizedBox(height: 16),
  131. // // 卡片底部 (点赞 评论 分享)
  132. Container(
  133. decoration: BoxDecoration(
  134. // color: Colors.white,
  135. border: Border(
  136. top: BorderSide(color: ColorUtils.string2Color('#EAEAEA'), width: 0.5),
  137. )
  138. ),
  139. padding: const EdgeInsets.only(top: 10, bottom: 15),
  140. child: NewsFeedCardFooter(
  141. key: UniqueKey(),
  142. isLike: item['isLike'],
  143. onLike: (){
  144. vm.handlerClickActionBtn('like', item);
  145. },
  146. onComment: (){
  147. vm.handlerClickActionBtn('comments', item);
  148. },
  149. onShare: (){
  150. vm.handlerClickActionBtn('share', item);
  151. },
  152. ),
  153. ),
  154. ]
  155. ),
  156. // 右上角 关注/取消关注 按钮
  157. Visibility(
  158. visible: !item['isFollow'],
  159. child: Positioned(
  160. right: 10,
  161. top: -5,
  162. child: Container(
  163. width: 83.5,
  164. height: 45.5,
  165. alignment: Alignment.center,
  166. // decoration: BoxDecoration(
  167. // color: ColorUtils.string2Color('#4161D0'),
  168. // borderRadius: BorderRadius.circular(5),
  169. // ),
  170. child: MyButton(
  171. text: '+Follow',
  172. textColor: Colors.white,
  173. backgroundColor: ColorUtils.string2Color('#4161D0'),
  174. radius: 8,
  175. minHeight: 27.5,
  176. padding: const EdgeInsets.only(left: 5, right: 5,top:9,bottom:9),
  177. fontWeight: FontWeight.w400,
  178. fontSize: 14,
  179. onPressed: (){
  180. // Navigator.pop(context);
  181. },
  182. ),
  183. )
  184. ),
  185. )
  186. ],
  187. ),
  188. ).constrained(
  189. width: double.infinity,
  190. // height: 580,
  191. // minHeight: 300,
  192. ).onTap((){
  193. vm.handlerClickActionBtn(null, item);
  194. });
  195. }
  196. }