services_page.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import 'package:cpt_services/modules/services/services_pageview_idx_data.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:auto_route/auto_route.dart';
  5. import 'package:flutter/rendering.dart';
  6. import 'package:flutter_hooks/flutter_hooks.dart';
  7. import 'package:hooks_riverpod/hooks_riverpod.dart';
  8. import 'package:plugin_basic/provider/user_config/user_config_service.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:shared/utils/color_utils.dart';
  11. import 'package:shared/utils/log_utils.dart';
  12. import 'package:widgets/custom_sliver_persistent_header_delegate.dart';
  13. import 'package:widgets/my_load_image.dart';
  14. import 'package:widgets/ext/ex_widget.dart';
  15. import 'package:widgets/my_text_view.dart';
  16. import 'package:widgets/my_appbar.dart';
  17. import 'package:cs_resources/theme/app_colors_theme.dart';
  18. import 'package:widgets/widget_export.dart';
  19. import '../../router/page/services_page_router.dart';
  20. import 'inProgress/in_progress_page.dart';
  21. import 'services_vm.dart';
  22. final tabsRouterKey = GlobalKey<AutoTabsRouterState>();
  23. final GlobalKey<ExtendedNestedScrollViewState> extendedNestedScrollViewKey =
  24. GlobalKey<ExtendedNestedScrollViewState>();
  25. @RoutePage()
  26. class ServicesPage extends HookConsumerWidget with WidgetsBindingObserver {
  27. int parentCategoryId = 0; //父级分类id
  28. ServicesPage({Key? key, required this.parentCategoryId}) : super(key: key);
  29. //启动当前页面
  30. static void startInstance({BuildContext? context, required int parentCategoryId}) {
  31. if (context != null) {
  32. context.router.push( ServicesPageRoute(parentCategoryId:parentCategoryId));
  33. } else {
  34. appRouter.push( ServicesPageRoute(parentCategoryId:parentCategoryId));
  35. }
  36. }
  37. bool _isKeyboardVisible = false;
  38. void handlerNestedScrollViewScroll({double? outerOffset, double? innerOffset, bool? isOuterScrollAnimated=false, bool? isInnerScrollAnimated=false}){
  39. if(outerOffset !=null){
  40. if(isOuterScrollAnimated!){
  41. extendedNestedScrollViewKey.currentState?.outerController.animateTo(
  42. outerOffset,
  43. duration: const Duration(seconds: 1),
  44. curve: Curves.easeIn,
  45. );
  46. }else {
  47. extendedNestedScrollViewKey.currentState?.outerController.jumpTo(
  48. outerOffset,
  49. );
  50. }
  51. }
  52. if(innerOffset !=null){
  53. extendedNestedScrollViewKey.currentState?.innerPositions.forEach((position) {
  54. if(isInnerScrollAnimated!){
  55. position.animateTo(innerOffset,
  56. duration: Duration(seconds: 1), curve: Curves.easeIn);
  57. }else {
  58. position.jumpTo(innerOffset);
  59. }
  60. });
  61. }
  62. }
  63. @override
  64. void didChangeMetrics() {
  65. final bottomInset = WidgetsBinding.instance.window.viewInsets.bottom;
  66. final newValue = bottomInset > 0.0;
  67. if (_isKeyboardVisible != newValue) {
  68. _isKeyboardVisible = newValue;
  69. if (_isKeyboardVisible) {
  70. handlerNestedScrollViewScroll(innerOffset: 0.0,);
  71. print("键盘已显示");
  72. } else {
  73. WidgetsBinding.instance.removeObserver(this);
  74. print("键盘已隐藏");
  75. }
  76. }
  77. }
  78. @override
  79. Widget build(BuildContext context, WidgetRef ref) {
  80. final vm = ref.read(servicesVmProvider.notifier);
  81. final state = ref.watch(servicesVmProvider);
  82. final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  83. useEffect(() {
  84. vm.setInitPageData(context, parentCategoryId);
  85. // 监听窗口
  86. WidgetsBinding.instance.addObserver(this);
  87. }, []);
  88. useEffect((){
  89. Log.d("ServicesPage initState");
  90. // 延迟监听
  91. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  92. if(tabsRouterKey.currentState?.controller != null){
  93. tabsRouterKey.currentState?.controller?.addListener((){
  94. vm.tabsRouterChange();
  95. });
  96. }
  97. });
  98. return (){
  99. Log.d("ServicesPage dispose");
  100. WidgetsBinding.instance.removeObserver(this);
  101. tabsRouterKey.currentState?.controller?.removeListener(vm.tabsRouterChange);
  102. };
  103. },[]);
  104. return Scaffold(
  105. appBar: MyAppBar.searchAppBar(
  106. context,
  107. value: vm.getCurrentQueryParams('keyword'),
  108. actions: [
  109. (state.currentPageViewIdx == 0) ? Container(
  110. child: const MyAssetImage(
  111. Assets.serviceServiceScoreYes,
  112. width: 21.5,
  113. height: 21.5,
  114. ).onTap((){
  115. vm.handlerClickNavbarLikeBtn(context);
  116. }),
  117. ):const SizedBox.shrink(),
  118. const SizedBox(width: 15),
  119. ],
  120. backgroundColor: context.appColors.backgroundWhite,
  121. onSearch: (value) {
  122. vm.handlerSearch(value);
  123. }
  124. ),
  125. backgroundColor: context.appColors.backgroundDefault,
  126. body: ExtendedNestedScrollView(
  127. key: extendedNestedScrollViewKey,
  128. onlyOneScrollInBody: true,
  129. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  130. return [
  131. SliverPersistentHeader(
  132. delegate: CustomSliverPersistentHeaderDelegate(
  133. maxHeight: 130,
  134. minHeight: 130,
  135. child: _buildTopSection(context, ref, vm, state),
  136. ),
  137. pinned: currentPageIdx !=0? true:false,
  138. ),
  139. // top 组件,转换为 Sliver 组件
  140. // SliverToBoxAdapter(
  141. // child: _buildTopSection(context, ref, vm, state),
  142. // ),
  143. ];
  144. },
  145. body: Column(
  146. mainAxisSize: MainAxisSize.max,
  147. children: [
  148. Expanded(
  149. child: AutoTabsRouter.pageView(
  150. key: tabsRouterKey,
  151. routes: const [
  152. HomeServicePageRoute(),
  153. InProgressPageRoute(),
  154. HistoryPageRoute(),
  155. ],
  156. builder: (context, child, pageController) {
  157. final tabsRouter = AutoTabsRouter.of(context);
  158. return Column(
  159. children: [
  160. Expanded(child: child),
  161. ],
  162. );
  163. },
  164. )
  165. )
  166. ]
  167. )
  168. )
  169. );
  170. }
  171. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, state) {
  172. final topSectionsData = vm.topSectionsData;
  173. final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  174. return Container(
  175. color: context.appColors.whiteBG,
  176. child: Center(
  177. child: Row(
  178. mainAxisSize: MainAxisSize.max,
  179. mainAxisAlignment: MainAxisAlignment.center,
  180. crossAxisAlignment: CrossAxisAlignment.center,
  181. children: List.generate(topSectionsData.length, (index) {
  182. final item = topSectionsData[index];
  183. return Column(
  184. children: [
  185. Container(
  186. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  187. height: 70,
  188. decoration: BoxDecoration(
  189. shape: BoxShape.circle, // 设置为圆形
  190. color: ColorUtils.string2Color("#F0F8FF"),
  191. boxShadow: index == currentPageIdx ? [
  192. BoxShadow(
  193. color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
  194. blurRadius: 5, // 设置模糊半径
  195. spreadRadius: 0.05, // 控制阴影扩散
  196. offset: const Offset(0, 4), // 设置阴影偏移量
  197. ),] : [],// 未选中时无阴影,
  198. ),
  199. child: MyAssetImage(
  200. item['icon'],
  201. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  202. // width: 70,
  203. height: 70,
  204. ).onTap(() {
  205. vm.handlerSwitchPageView(context, index);
  206. },
  207. type: ClickType.throttle,
  208. ),
  209. ),
  210. SizedBox.fromSize(size: const Size(0, 9)),
  211. MyTextView(
  212. item['title'],
  213. fontSize: 15,
  214. textColor: index == currentPageIdx ? ColorUtils.string2Color('#4161D0'): context.appColors.textBlack,
  215. textAlign: TextAlign.center,
  216. isFontMedium: true,
  217. ),
  218. ],
  219. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  220. }),
  221. ),
  222. ),
  223. );
  224. }
  225. }