property_news_page.dart 6.9 KB

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