property_news_page.dart 7.1 KB

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