services_page.dart 9.1 KB

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