property_news_page.dart 8.4 KB

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