property_news_page.dart 6.4 KB

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