services_main_page.dart 6.0 KB

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