services_main_page.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: const SliverGridDelegateWithFixedCrossAxisCount(
  79. crossAxisCount: 2,
  80. mainAxisSpacing: 10,
  81. crossAxisSpacing: 5,
  82. childAspectRatio: 165/146, //宽高比
  83. ),
  84. delegate: SliverChildBuilderDelegate(
  85. (context, index) {
  86. return Padding(
  87. padding: const EdgeInsets.all(10.0),
  88. child: _buildCardItem(context, ref, state.datas?[index] as ServiceCategoryEntity, vm).onTap((){
  89. final data = state.datas?[index] as ServiceCategoryEntity;
  90. if (data.id != null && data.type != null) {
  91. vm.goToPaidOrRepairListPage(context, data.id!, data.type!, data);
  92. }
  93. }),
  94. );
  95. },
  96. childCount: state.datas?.length,
  97. ),
  98. ),
  99. ]
  100. ),
  101. ),
  102. ),
  103. ],
  104. ),
  105. );
  106. }
  107. Widget _buildCardItem(BuildContext context, WidgetRef ref, ServiceCategoryEntity data, ServiceMainVm vm){
  108. if(data?.id == 1){
  109. // home Services
  110. return _buildServiceCard(
  111. context,
  112. ServicesType.paid,
  113. Assets.serviceHomeServices,
  114. "Home Services",
  115. 102.5,
  116. 82.5
  117. );
  118. }else if(data?.id == 4){
  119. // maintenance
  120. return _buildServiceCard(
  121. context,
  122. ServicesType.inquiry,
  123. Assets.serviceMaintenance,
  124. "Maintenance",
  125. 102.5,
  126. 82.5
  127. );
  128. }else {
  129. return _buildServiceCard(
  130. context,
  131. ServicesType.paid,
  132. Assets.serviceHomeServices,
  133. "Home Services",
  134. 102.5,
  135. 82.5
  136. );
  137. }
  138. }
  139. Widget _buildServiceCard(
  140. BuildContext context,
  141. ServicesType type,
  142. String imagePath,
  143. String title,
  144. double iconWidth,
  145. double iconHeight,
  146. ) {
  147. return Container(
  148. margin: EdgeInsets.all(10),
  149. decoration: BoxDecoration(
  150. color: context.appColors.whiteBG,
  151. borderRadius: BorderRadius.circular(10),
  152. boxShadow: [
  153. BoxShadow(
  154. color: context.appColors.whiteBG,
  155. blurRadius: 5,
  156. offset: Offset(0, 3),
  157. ),
  158. ],
  159. ),
  160. child: Stack(
  161. children: [
  162. Padding(
  163. padding: const EdgeInsets.only(left: 15, right:15.0, top: 10,bottom: 10),
  164. child: MyTextView(
  165. title,
  166. fontSize: 16,
  167. isFontMedium: true,
  168. alignment: Alignment.topLeft,
  169. ),
  170. ),
  171. Positioned(
  172. bottom: 0,
  173. right: 0,
  174. child: Container(
  175. child: MyLoadImage(
  176. imagePath,
  177. fit: BoxFit.fitWidth,
  178. width: iconWidth,
  179. height: iconHeight,
  180. ),
  181. ),
  182. )
  183. ]
  184. )
  185. );
  186. }
  187. }