import 'package:cpt_property/modules/property/page/property_page.dart'; import 'package:flutter/material.dart'; import 'package:auto_route/auto_route.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:router/ext/auto_router_extensions.dart'; import 'package:shared/utils/log_utils.dart'; import 'package:shared/utils/util.dart'; import 'package:widgets/ext/ex_widget.dart'; import 'package:widgets/load_state_layout.dart'; import 'package:widgets/my_load_image.dart'; import 'package:widgets/my_text_view.dart'; import 'package:widgets/widget_export.dart'; import 'package:cs_resources/generated/assets.dart'; import '../../../router/page/property_page_router.dart'; import '../vm/property_news_vm.dart'; @RoutePage() class PropertyNewsPage extends HookConsumerWidget { const PropertyNewsPage({Key? key}) : super(key: key); //启动当前页面 static void startInstance({BuildContext? context}) { if (context != null) { context.router.push(const PropertyNewsPageRoute()); } else { appRouter.push(const PropertyNewsPageRoute()); } } Widget _buildItemLeftSection(BuildContext context,WidgetRef ref, item, _vm) { return Container( // color: Colors.blue, width: 100, // height: 117.5, child: Center( child: MyLoadImage( item['pic'], placeholderPath: Assets.propertyNewsItemBg, width: 60.5, height: 50.5, ), ) ).marginOnly(right: 17.5).constrained( minHeight: 117.5, ); } Widget _buildItemRightSection(BuildContext context,WidgetRef ref, item, _vm) { // 使用 ref.select 监听 list 中 item 的 Map 对象中的 isCollection 字段 final isCollection = ref.watch(propertyNewsVmProvider.select((state) { final curItem = state.list.firstWhere( (valueItem) => valueItem['id'] == item['id'], orElse: () => {'isCollection': false}, ); return curItem['isCollection'] as bool; })); return Container( color: Colors.white, padding: const EdgeInsets.only(left:5, top: 10.5, bottom: 10.5), child: Container( child: Stack( children: [ Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Expanded( child: MyTextView( item['title'], maxLines: 2, // 设置最大行数为2 isTextEllipsis: true, // 超出部分用省略号表示 fontSize: 16, textColor: Colors.black, isFontMedium: true, ), ) ], ), const SizedBox(height: 10.5), Row( children: [ Expanded( child: MyTextView( item['description'], maxLines: 2, // 设置最大行数为2 isTextEllipsis: true, // 超出部分用省略号表示 fontSize: 12, textColor: Colors.black, isFontRegular: true, ) ) ] ), const SizedBox(height: 12.5), Row( children: [ Expanded( child: MyTextView( Utils.getTimeAgo(item['time']), maxLines: 1, // 设置最大行数为2 isTextEllipsis: true, // 超出部分用省略号表示 fontSize: 12, textColor: Colors.black, isFontRegular: true, ), ) ] ), ], ), Positioned( right: 5, bottom: 0, child: MyAssetImage(isCollection? Assets.propertyCollectionActive:Assets.propertyCollection,width:22,height: 20.5,).onTap((){ Log.d("点击了收藏按钮"); _vm.handlerCollection(item, isCollection); }), ), ], ).constrained( minHeight: 96.5, ), ), ); } // listitem Widget _buildNewsItem(BuildContext context,WidgetRef ref, item, _vm) { return Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: [ Container( width: MediaQuery.of(context).size.width - 30, margin: const EdgeInsets.only(left: 15,right: 15,top: 12.5), child: Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildItemLeftSection(context, ref, item, _vm), Expanded(child: _buildItemRightSection(context, ref, item, _vm)), ], ), ).constrained( minHeight: 117.5, ), ], ).onTap((){ // 去详情 _vm.goNewsDetail(item); }); } @override Widget build(BuildContext context, WidgetRef ref) { final _vm = ref.read(propertyNewsVmProvider.notifier); final state = ref.watch(propertyNewsVmProvider); useEffect(() { // 组件挂载时执行 - 执行接口请求 Future.microtask(() => _vm.initPageData()); return () { // 组件卸载时执行 Log.d("property_news_page 组件卸载时执行"); }; }, []); return Scaffold( // appBar: AppBar(title: Text("资产")), body: SizedBox( width: double.infinity, height: double.infinity, child: EasyRefresh( controller: _vm.refreshController, // 上拉加载 onLoad: () async{ Log.d("----onLoad"); _vm.loadMore(); }, // 下拉刷新 onRefresh: () async{ Log.d("----onRefresh"); _vm.onRefresh(); }, child: LoadStateLayout( state: state.loadingState, errorMessage: state.errorMessage, errorRetry: () { _vm.retryRequest(); }, successSliverWidget: [ SliverList( delegate: SliverChildBuilderDelegate( (context, index){ return _buildNewsItem(context, ref, state.list[index], _vm); }, childCount: state.list.length ) ) ], ), ).marginOnly(top: 5, bottom: 5) ), ); } }