property_news_page.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import 'package:cpt_property/modules/property/page/property_page.dart';
  2. import 'package:cs_resources/theme/app_colors_theme.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:auto_route/auto_route.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:shared/utils/util.dart';
  11. import 'package:widgets/ext/ex_widget.dart';
  12. import 'package:widgets/load_state_layout.dart';
  13. import 'package:widgets/my_load_image.dart';
  14. import 'package:widgets/my_text_view.dart';
  15. import 'package:widgets/widget_export.dart';
  16. import 'package:cs_resources/generated/assets.dart';
  17. import '../../../router/page/property_page_router.dart';
  18. import '../vm/property_news_vm.dart';
  19. @RoutePage()
  20. class PropertyNewsPage extends HookConsumerWidget {
  21. const PropertyNewsPage({Key? key}) : super(key: key);
  22. //启动当前页面
  23. static void startInstance({BuildContext? context}) {
  24. if (context != null) {
  25. context.router.push(const PropertyNewsPageRoute());
  26. } else {
  27. appRouter.push(const PropertyNewsPageRoute());
  28. }
  29. }
  30. static double cardHeight = 117.5 + 50;
  31. static double cardLeftWidth = 125;
  32. Widget _buildItemLeftSection(BuildContext context,WidgetRef ref, item, vm) {
  33. // final imageHeight = item['pic']!.isNotEmpty? cardHeight : cardHeight;
  34. return Container(
  35. height: cardHeight,
  36. width: cardLeftWidth,
  37. decoration: BoxDecoration(
  38. color: ColorUtils.string2Color('#F2F3F6'),
  39. // color: Colors.red,
  40. borderRadius: const BorderRadius.only(
  41. topLeft: Radius.circular(8),
  42. bottomLeft: Radius.circular(8),
  43. ),
  44. ),
  45. child: Center(
  46. child: MyLoadImage(
  47. item['cover_image'],
  48. placeholderPath: Assets.propertyNewsItemBg,
  49. height: item['cover_image']!.isNotEmpty? cardHeight : 60.5,
  50. width: item['cover_image']!.isNotEmpty? cardLeftWidth : 60.5,
  51. // fit: BoxFit.cover,
  52. fit: BoxFit.contain,
  53. cornerRadius: 8,
  54. ),
  55. )
  56. );
  57. }
  58. Widget _buildItemRightSection(BuildContext context,WidgetRef ref, item, vm) {
  59. // 使用 ref.select 监听 list 中 item 的 Map 对象中的 liked 字段
  60. final liked = ref.watch(propertyNewsVmProvider.select((state) {
  61. final curItem = state.list!.firstWhere(
  62. (valueItem) => valueItem['id'] == item['id'],
  63. orElse: () => {'liked': false},
  64. );
  65. return curItem['liked'] as bool;
  66. }));
  67. return Container(
  68. padding: const EdgeInsets.only(left:17.5,right: 17.5,top: 13, bottom: 13),
  69. decoration: BoxDecoration(
  70. color: context.appColors.backgroundWhite,
  71. borderRadius: const BorderRadius.only(
  72. topRight: Radius.circular(8),
  73. bottomRight: Radius.circular(8),
  74. ),
  75. ),
  76. child: Stack(
  77. children: [
  78. Column(
  79. mainAxisAlignment: MainAxisAlignment.start,
  80. crossAxisAlignment: CrossAxisAlignment.start,
  81. children: [
  82. Row(
  83. children: [
  84. Expanded(
  85. child: MyTextView(
  86. item['title'],
  87. maxLines: 2, // 设置最大行数为2
  88. isTextEllipsis: true, // 超出部分用省略号表示
  89. fontSize: 16,
  90. textColor: context.appColors.textBlack,
  91. isFontMedium: true,
  92. ),
  93. )
  94. ],
  95. ),
  96. const SizedBox(height: 10.5),
  97. Row(
  98. children: [
  99. Expanded(
  100. child: MyTextView(
  101. item['content'],
  102. maxLines: 2, // 设置最大行数为2
  103. isTextEllipsis: true, // 超出部分用省略号表示
  104. fontSize: 12,
  105. textColor: context.appColors.textBlack,
  106. isFontRegular: true,
  107. )
  108. )
  109. ]
  110. ),
  111. const SizedBox(height: 12.5),
  112. Row(
  113. children: [
  114. Expanded(
  115. child: MyTextView(
  116. Utils.getTimeAgo(item['created_at']),
  117. maxLines: 1, // 设置最大行数为1
  118. isTextEllipsis: true, // 超出部分用省略号表示
  119. fontSize: 12,
  120. textColor: context.appColors.textBlack,
  121. isFontRegular: true,
  122. ),
  123. )
  124. ]
  125. ),
  126. ],
  127. ),
  128. Positioned(
  129. right: 5,
  130. bottom: 0,
  131. child: MyAssetImage(liked? Assets.propertyCollectionActive:Assets.propertyCollection,width:22.5,height: 21,).onTap((){
  132. Log.d("点击了收藏按钮");
  133. vm.handlerCollection(item, liked);
  134. }),
  135. ),
  136. ],
  137. ),
  138. ).constrained(
  139. minHeight: cardHeight
  140. );
  141. }
  142. // listitem
  143. Widget _buildNewsItem(BuildContext context,WidgetRef ref, item, vm) {
  144. return Row(
  145. mainAxisAlignment: MainAxisAlignment.center,
  146. crossAxisAlignment: CrossAxisAlignment.center,
  147. mainAxisSize: MainAxisSize.max,
  148. children: [
  149. Container(
  150. width: MediaQuery.of(context).size.width - 30,
  151. margin: const EdgeInsets.only(left: 15,right: 15,top: 12.5),
  152. decoration: BoxDecoration(
  153. borderRadius: BorderRadius.circular(10),
  154. boxShadow: [
  155. BoxShadow(
  156. color: ColorUtils.string2Color('#000000').withOpacity(0.05),
  157. offset: const Offset(0, 0),
  158. blurRadius: 10,
  159. spreadRadius: 0,
  160. ),
  161. ]
  162. ),
  163. child: Row(
  164. mainAxisAlignment: MainAxisAlignment.start,
  165. crossAxisAlignment: CrossAxisAlignment.start,
  166. children: [
  167. _buildItemLeftSection(context, ref, item, vm),
  168. Expanded(child: _buildItemRightSection(context, ref, item, vm)),
  169. ],
  170. ),
  171. ).constrained(
  172. minHeight: 117.5,
  173. ),
  174. ],
  175. );
  176. }
  177. @override
  178. Widget build(BuildContext context, WidgetRef ref) {
  179. final vm = ref.read(propertyNewsVmProvider.notifier);
  180. final state = ref.watch(propertyNewsVmProvider);
  181. useEffect(() {
  182. // 组件挂载时执行 - 执行接口请求
  183. Future.microtask(() => vm.initPageData());
  184. return () {
  185. // 组件卸载时执行
  186. Log.d("property_news_page 组件卸载时执行");
  187. };
  188. }, []);
  189. return Scaffold(
  190. // appBar: AppBar(title: Text("资产")),
  191. // backgroundColor: context.appColors.backgroundDefault,
  192. backgroundColor: ColorUtils.string2Color("#F2F3F6"),
  193. body: SizedBox(
  194. width: double.infinity,
  195. height: double.infinity,
  196. child: EasyRefresh(
  197. controller: vm.refreshController,
  198. // 上拉加载
  199. onLoad: () async{
  200. Log.d("----onLoad");
  201. vm.loadMore();
  202. },
  203. // 下拉刷新
  204. onRefresh: () async{
  205. Log.d("----onRefresh");
  206. vm.onRefresh();
  207. },
  208. child: LoadStateLayout(
  209. state: state.loadingState,
  210. errorMessage: state.errorMessage,
  211. errorRetry: () {
  212. vm.retryRequest();
  213. },
  214. successSliverWidget: [
  215. SliverList(
  216. delegate: SliverChildBuilderDelegate(
  217. (context, index){
  218. return _buildNewsItem(context, ref, state.list![index], vm).onTap((){
  219. Log.d("点击了item");
  220. vm.goNewsDetailPage(context, state.list![index]['id']);
  221. });
  222. },
  223. childCount: state.list!.length
  224. )
  225. )
  226. ],
  227. ),
  228. ).marginOnly(top: 5, bottom: 5)
  229. ),
  230. );
  231. }
  232. }