property_news_page.dart 6.4 KB

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