services_main_page.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import 'package:cpt_services/modules/services/services_main_vm.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:cs_resources/generated/l10n.dart';
  4. import 'package:domain/entity/service_category_entity.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:auto_route/auto_route.dart';
  7. import 'package:flutter/rendering.dart';
  8. import 'package:flutter_hooks/flutter_hooks.dart';
  9. import 'package:hooks_riverpod/hooks_riverpod.dart';
  10. import 'package:plugin_basic/provider/user_config/user_config_service.dart';
  11. import 'package:router/ext/auto_router_extensions.dart';
  12. import 'package:shared/utils/color_utils.dart';
  13. import 'package:shared/utils/ext_dart.dart';
  14. import 'package:shared/utils/log_utils.dart';
  15. import 'package:widgets/custom_sliver_persistent_header_delegate.dart';
  16. import 'package:widgets/load_state_layout.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. enum ServicesType {
  25. paid,
  26. inquiry,
  27. }
  28. @RoutePage()
  29. class ServicesMainPage extends HookConsumerWidget {
  30. ServicesMainPage({Key? key}) : super(key: key);
  31. //启动当前页面
  32. static void startInstance({BuildContext? context}) {
  33. if (context != null) {
  34. context.router.push( ServicesMainPageRoute());
  35. } else {
  36. appRouter.push( ServicesMainPageRoute());
  37. }
  38. }
  39. @override
  40. Widget build(BuildContext context, WidgetRef ref) {
  41. final vm = ref.read(serviceMainVmProvider.notifier);
  42. final state = ref.watch(serviceMainVmProvider);
  43. useEffect(() {
  44. Log.d("ServicesMainPage initState");
  45. // 组件挂载时执行 - 执行接口请求
  46. Future.microtask(() => vm.initPageData());
  47. return () {
  48. Log.d("ServicesMainPage dispose");
  49. };
  50. }, []);
  51. return Scaffold(
  52. appBar: MyAppBar.appBar(
  53. context,
  54. S.current.service,
  55. backgroundColor: context.appColors.whiteBG,
  56. ),
  57. backgroundColor: ColorUtils.string2Color("#F2F3F6"),
  58. body: Column(
  59. children: [
  60. Expanded(
  61. child: EasyRefresh(
  62. controller: vm.refreshController,
  63. // 上拉加载
  64. onLoad: null,
  65. // 下拉刷新
  66. onRefresh: () async{
  67. Log.d("----onRefresh");
  68. vm.onRefresh();
  69. },
  70. child: LoadStateLayout(
  71. state: state.loadingState!,
  72. errorMessage: state.errorMessage,
  73. errorRetry: () {
  74. vm.retryRequest();
  75. },
  76. successSliverWidget:[
  77. SliverGrid(
  78. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  79. crossAxisCount: 2,
  80. mainAxisSpacing: 15,
  81. crossAxisSpacing: 15,
  82. // childAspectRatio: 165/146, //宽高比
  83. childAspectRatio: 165/160 * ((165/160).ap), // 宽高比
  84. ),
  85. delegate: SliverChildBuilderDelegate(
  86. (context, index) {
  87. return Padding(
  88. padding: const EdgeInsets.all(10.0),
  89. child: _buildCardItem(context, ref, state.datas?[index] as ServiceCategoryEntity, vm).onTap((){
  90. final data = state.datas?[index] as ServiceCategoryEntity;
  91. if (data.id != null && data.type != null) {
  92. vm.goToPaidOrRepairListPage(context, data.id!, data.type!, data);
  93. }
  94. }),
  95. );
  96. },
  97. childCount: state.datas?.length,
  98. ),
  99. ),
  100. ]
  101. ),
  102. ),
  103. ),
  104. ],
  105. ),
  106. );
  107. }
  108. Widget _buildCardItem(BuildContext context, WidgetRef ref, ServiceCategoryEntity data, ServiceMainVm vm){
  109. if(data?.id == 1){
  110. // home Services
  111. return _buildServiceCard(
  112. context,
  113. ServicesType.paid,
  114. Assets.serviceHomeServices,
  115. "Home Services",
  116. 102.5,
  117. 82.5
  118. );
  119. }else if(data?.id == 4){
  120. // maintenance
  121. return _buildServiceCard(
  122. context,
  123. ServicesType.inquiry,
  124. Assets.serviceMaintenance,
  125. "Maintenance",
  126. 102.5,
  127. 82.5
  128. );
  129. }else {
  130. return _buildServiceCard(
  131. context,
  132. ServicesType.paid,
  133. Assets.serviceHomeServices,
  134. "Home Services",
  135. 102.5,
  136. 82.5
  137. );
  138. }
  139. }
  140. Widget _buildServiceCard(
  141. BuildContext context,
  142. ServicesType type,
  143. String imagePath,
  144. String title,
  145. double iconWidth,
  146. double iconHeight,
  147. ) {
  148. return Container(
  149. width: double.infinity,
  150. padding: const EdgeInsets.only(left: 10, right: 10),
  151. decoration: BoxDecoration(
  152. color: context.appColors.whiteBG,
  153. borderRadius: BorderRadius.circular(10),
  154. boxShadow: [
  155. BoxShadow(
  156. color: context.appColors.whiteBG,
  157. blurRadius: 5,
  158. offset: Offset(0, 3),
  159. ),
  160. ],
  161. ),
  162. child: Stack(
  163. children: [
  164. Padding(
  165. padding: const EdgeInsets.only(left: 15, right:15.0, top: 10,bottom: 10),
  166. child: MyTextView(
  167. title,
  168. fontSize: 16,
  169. isFontMedium: true,
  170. alignment: Alignment.topLeft,
  171. ),
  172. ),
  173. Positioned(
  174. bottom: 0,
  175. right: 0,
  176. child: Container(
  177. child: MyLoadImage(
  178. imagePath,
  179. fit: BoxFit.fitWidth,
  180. width: iconWidth,
  181. height: iconHeight,
  182. ),
  183. ),
  184. )
  185. ]
  186. )
  187. );
  188. }
  189. }