property_news_page.dart 6.3 KB

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