services_main_page.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import 'package:cpt_services/modules/services/services_main_vm.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:domain/entity/service_category_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/user_config/user_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/log_utils.dart';
  13. import 'package:widgets/custom_sliver_persistent_header_delegate.dart';
  14. import 'package:widgets/load_state_layout.dart';
  15. import 'package:widgets/my_load_image.dart';
  16. import 'package:widgets/ext/ex_widget.dart';
  17. import 'package:widgets/my_text_view.dart';
  18. import 'package:widgets/my_appbar.dart';
  19. import 'package:cs_resources/theme/app_colors_theme.dart';
  20. import 'package:widgets/widget_export.dart';
  21. import '../../router/page/services_page_router.dart';
  22. import 'inProgress/in_progress_page.dart';
  23. import 'repair/repair_page.dart';
  24. import 'services_page.dart';
  25. enum ServicesType {
  26. paid,
  27. inquiry,
  28. }
  29. @RoutePage()
  30. class ServicesMainPage extends HookConsumerWidget {
  31. ServicesMainPage({Key? key}) : super(key: key);
  32. //启动当前页面
  33. static void startInstance({BuildContext? context}) {
  34. if (context != null) {
  35. context.router.push( ServicesMainPageRoute());
  36. } else {
  37. appRouter.push( ServicesMainPageRoute());
  38. }
  39. }
  40. @override
  41. Widget build(BuildContext context, WidgetRef ref) {
  42. final vm = ref.read(serviceMainVmProvider.notifier);
  43. final state = ref.watch(serviceMainVmProvider);
  44. useEffect(() {
  45. Log.d("ServicesMainPage initState");
  46. // 组件挂载时执行 - 执行接口请求
  47. Future.microtask(() => vm.initPageData());
  48. return () {
  49. Log.d("ServicesMainPage dispose");
  50. };
  51. }, []);
  52. return Scaffold(
  53. appBar: MyAppBar.appBar(
  54. context,
  55. "Service",
  56. backgroundColor: context.appColors.whiteBG,
  57. ),
  58. backgroundColor: ColorUtils.string2Color("#F2F3F6"),
  59. body: Column(
  60. children: [
  61. Expanded(
  62. child: EasyRefresh(
  63. controller: vm.refreshController,
  64. // 上拉加载
  65. onLoad: null,
  66. // 下拉刷新
  67. onRefresh: () async{
  68. Log.d("----onRefresh");
  69. vm.onRefresh();
  70. },
  71. child: LoadStateLayout(
  72. state: state.loadingState!,
  73. errorMessage: state.errorMessage,
  74. errorRetry: () {
  75. vm.retryRequest();
  76. },
  77. successSliverWidget:[
  78. SliverGrid(
  79. gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
  80. crossAxisCount: 2,
  81. mainAxisSpacing: 10,
  82. crossAxisSpacing: 10,
  83. childAspectRatio: 165/146, //宽高比
  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. 129,
  127. 107.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. margin: EdgeInsets.all(10),
  150. decoration: BoxDecoration(
  151. color: context.appColors.whiteBG,
  152. borderRadius: BorderRadius.circular(10),
  153. boxShadow: [
  154. BoxShadow(
  155. color: context.appColors.whiteBG,
  156. blurRadius: 5,
  157. offset: Offset(0, 3),
  158. ),
  159. ],
  160. ),
  161. child: Stack(
  162. children: [
  163. Padding(
  164. padding: const EdgeInsets.only(left: 15, right:15.0, top: 10,bottom: 10),
  165. child: MyTextView(
  166. title,
  167. fontSize: 16,
  168. isFontMedium: true,
  169. alignment: Alignment.topLeft,
  170. ),
  171. ),
  172. Positioned(
  173. bottom: 0,
  174. right: 0,
  175. child: Container(
  176. child: MyLoadImage(
  177. imagePath,
  178. fit: BoxFit.cover,
  179. width: iconWidth,
  180. height: iconHeight,
  181. ),
  182. ),
  183. )
  184. ]
  185. )
  186. );
  187. }
  188. }