home_service_page.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:domain/entity/garage_sale_rent_entity.dart';
  3. import 'package:domain/entity/paid_service_entity.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:auto_route/auto_route.dart';
  6. import 'package:flutter/rendering.dart';
  7. import 'package:flutter_hooks/flutter_hooks.dart';
  8. import 'package:hooks_riverpod/hooks_riverpod.dart';
  9. import 'package:plugin_basic/provider/app_config/app_config_service.dart';
  10. import 'package:router/ext/auto_router_extensions.dart';
  11. import 'package:shared/utils/color_utils.dart';
  12. import 'package:shared/utils/ext_dart.dart';
  13. import 'package:shared/utils/log_utils.dart';
  14. import 'package:shared/utils/screen_util.dart';
  15. import 'package:shared/utils/size_config.dart';
  16. import 'package:widgets/load_state_layout.dart';
  17. import 'package:widgets/my_button.dart';
  18. import 'package:widgets/my_load_image.dart';
  19. import 'package:widgets/ext/ex_widget.dart';
  20. import 'package:widgets/my_text_view.dart';
  21. import 'package:widgets/my_appbar.dart';
  22. import 'package:cs_resources/theme/app_colors_theme.dart';
  23. import 'package:widgets/widget_export.dart';
  24. import '../../../router/page/services_page_router.dart';
  25. import 'home_service_vm.dart';
  26. import 'service_card_item.dart';
  27. @RoutePage()
  28. class HomeServicePage extends HookConsumerWidget {
  29. const HomeServicePage({Key? key}) : super(key: key);
  30. //启动当前页面
  31. static void startInstance({BuildContext? context}) {
  32. if (context != null) {
  33. context.router.push(const HomeServicePageRoute());
  34. } else {
  35. appRouter.push(const HomeServicePageRoute());
  36. }
  37. }
  38. @override
  39. Widget build(BuildContext context, WidgetRef ref) {
  40. final vm = ref.read(homeServiceVmProvider.notifier);
  41. final state = ref.watch(homeServiceVmProvider);
  42. // final appConfigState = ref.watch(appConfigServiceProvider)
  43. useEffect(() {
  44. // 组件挂载时执行 - 执行接口请求
  45. Future.microtask(() => vm.initPageData());
  46. return () {
  47. // 组件卸载时执行
  48. Log.d("garage_homeService_page 组件卸载时执行");
  49. };
  50. }, []);
  51. return Scaffold(
  52. // appBar: MyAppBar.appBar(
  53. // context,
  54. // "HomeService",
  55. // backgroundColor: context.appColors.whiteBG,
  56. // ),
  57. backgroundColor: ColorUtils.string2Color("#F2F3F6"),
  58. body: SizedBox(
  59. width: double.infinity,
  60. height: double.infinity,
  61. child: Column(
  62. children: [
  63. // sort and filter
  64. _buildSortAndFilter(context, ref, vm, state),
  65. Expanded(
  66. child: EasyRefresh(
  67. key: ValueKey('homeService'),
  68. controller: vm.refreshController,
  69. // 上拉加载
  70. onLoad: () async{
  71. Log.d("----onLoad");
  72. vm.loadMore();
  73. },
  74. // 下拉刷新
  75. onRefresh: () async{
  76. Log.d("----onRefresh");
  77. vm.onRefresh();
  78. },
  79. child: LoadStateLayout(
  80. state: state.loadingState,
  81. errorMessage: state.errorMessage,
  82. errorRetry: () {
  83. vm.retryRequest();
  84. },
  85. successSliverWidget:[
  86. SliverGrid(
  87. gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
  88. crossAxisCount: 2, // 每行显示两个项目
  89. mainAxisSpacing: 15,
  90. crossAxisSpacing: 15,
  91. childAspectRatio: 172.5/170, // 宽高比
  92. // childAspectRatio: 172.5/170 * ScreenUtil.getAdapterSizeCtx(context, 172.5/170), // 宽高比
  93. // childAspectRatio: 172.5/170 * ((172.5/170).ap), // 宽高比
  94. ),
  95. delegate: SliverChildBuilderDelegate(
  96. (context, index) {
  97. return _buildHomeServiceItem(context, ref, state.list[index] as PaidServiceList, vm).onTap((){
  98. // 去详情页面
  99. PaidServiceList paidServiceEntity = state.list[index] as PaidServiceList;
  100. PaidServiceListCategory paidServiceCategory = paidServiceEntity.category!;
  101. vm.handlerGotoDetail(context: context, id: paidServiceEntity.id!, paidServiceCategory: paidServiceCategory!, liked: paidServiceEntity.liked!, likesCount: paidServiceEntity.likesCount!);
  102. });
  103. },
  104. childCount: state.list.length,
  105. ),
  106. ),
  107. ],
  108. ),
  109. ).marginOnly(left: 15,right: 15,top: 15,bottom: 15),
  110. ),
  111. ],
  112. )
  113. ),
  114. );
  115. }
  116. Widget _buildHomeServiceItem(BuildContext context, WidgetRef ref, PaidServiceList item, vm){
  117. return SizedBox(
  118. width: double.infinity,
  119. // height: 170,
  120. child: Container(
  121. decoration: BoxDecoration(
  122. color: context.appColors.whiteBG,
  123. borderRadius: BorderRadius.circular(8),
  124. boxShadow: [
  125. BoxShadow(
  126. color: ColorUtils.string2Color('#E5E5E5'),
  127. offset: const Offset(0, 2),
  128. blurRadius: 8,
  129. ),
  130. ],
  131. ),
  132. child: Column(
  133. mainAxisAlignment: MainAxisAlignment.start,
  134. crossAxisAlignment: CrossAxisAlignment.start,
  135. mainAxisSize: MainAxisSize.max,
  136. children: [
  137. Expanded(
  138. child: HomeServiceCard(
  139. key: UniqueKey(),
  140. itemEntity: item,
  141. cleanServiceType: item.category!.type,
  142. onClickColleciotn: (dynamic collectionValue) async {
  143. Log.d("点击了喜欢按钮 --id:${item.id}- $collectionValue");
  144. int id = item.id!;
  145. return await vm.handlerClickCollection(id, collectionValue);
  146. }
  147. ),
  148. ),
  149. ]
  150. ),
  151. ),
  152. );
  153. }
  154. Widget _buildSortAndFilter(BuildContext context, WidgetRef ref, vm, state) {
  155. final sortTitle = ref.watch(homeServiceVmProvider.select((state) => state.sortBySelectedOption));
  156. final expandSortIcon = useState<bool>(false);
  157. return Container(
  158. padding: const EdgeInsets.only(left: 15, right: 15, top: 10, bottom: 10),
  159. color: context.appColors.whiteBG,
  160. child: Row(
  161. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  162. crossAxisAlignment: CrossAxisAlignment.center,
  163. children: [
  164. // 筛选
  165. Row(
  166. children: [
  167. MyTextView(
  168. "Sort",
  169. fontSize: 15,
  170. isFontRegular: true,
  171. textAlign: TextAlign.center,
  172. ),
  173. Wrap(
  174. crossAxisAlignment: WrapCrossAlignment.center,
  175. children: [
  176. Row(
  177. children: [
  178. sortTitle != null? MyTextView(
  179. "${sortTitle}",
  180. fontSize: 13,
  181. isFontRegular: true,
  182. textAlign: TextAlign.center,
  183. marginLeft: 15,
  184. textColor: context.appColors.textPrimary,
  185. ): const SizedBox(),
  186. IconButton(
  187. // padding: EdgeInsets.zero,
  188. constraints: const BoxConstraints(),
  189. onPressed: null,
  190. icon: Icon(
  191. expandSortIcon.value ? Icons.arrow_drop_up: Icons.arrow_drop_down
  192. ),
  193. ),
  194. ],
  195. ),
  196. ],
  197. ),
  198. ],
  199. ).onTap((){
  200. Log.d("点击了sort");
  201. vm.handlerClickSortIcon(context);
  202. expandSortIcon.value = !expandSortIcon.value;
  203. }),
  204. // 排序
  205. const MyAssetImage(
  206. Assets.serviceFilterIcon,
  207. width: 20.5,
  208. height: 20.9,
  209. ).onTap((){
  210. Log.d("点击了filter");
  211. vm.handlerClickFilterIcon(context);
  212. }),
  213. ]
  214. ),
  215. );
  216. }
  217. _buildCustomSort(BuildContext context, WidgetRef ref, vm, state) {
  218. return Container();
  219. }
  220. }