services_main_page.dart 6.0 KB

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