home_service_page.dart 8.3 KB

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