services_page.dart 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. final isCollection = useState<int?>(null);
  85. useEffect(() {
  86. vm.setInitPageData(context, parentCategoryId);
  87. // 监听窗口
  88. WidgetsBinding.instance.addObserver(this);
  89. }, []);
  90. useEffect((){
  91. Log.d("ServicesPage initState");
  92. // 延迟监听
  93. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  94. if(tabsRouterKey.currentState?.controller != null){
  95. tabsRouterKey.currentState?.controller?.addListener((){
  96. vm.tabsRouterChange();
  97. });
  98. }
  99. });
  100. return (){
  101. Log.d("ServicesPage dispose");
  102. WidgetsBinding.instance.removeObserver(this);
  103. tabsRouterKey.currentState?.controller?.removeListener(vm.tabsRouterChange);
  104. };
  105. },[]);
  106. return Scaffold(
  107. appBar: MyAppBar.searchAppBar(
  108. context,
  109. value: vm.getCurrentQueryParams('keyword'),
  110. actions: [
  111. (state.currentPageViewIdx == 0) ? Container(
  112. child: MyAssetImage(
  113. isCollection.value ==1? Assets.serviceServiceScoreYes : Assets.serviceServiceScoreNo,
  114. width: 21.5,
  115. height: 21.5,
  116. ).onTap((){
  117. isCollection.value = isCollection.value == 1 ? null:1;
  118. vm.handlerClickNavbarLikeBtn(context, isCollection.value);
  119. }),
  120. ):const SizedBox.shrink(),
  121. const SizedBox(width: 15),
  122. ],
  123. backgroundColor: context.appColors.backgroundWhite,
  124. onSearch: (value) {
  125. vm.handlerSearch(value);
  126. }
  127. ),
  128. backgroundColor: context.appColors.backgroundDefault,
  129. body: ExtendedNestedScrollView(
  130. key: extendedNestedScrollViewKey,
  131. onlyOneScrollInBody: true,
  132. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  133. return [
  134. SliverPersistentHeader(
  135. delegate: CustomSliverPersistentHeaderDelegate(
  136. maxHeight: 120,
  137. minHeight: 120,
  138. child: _buildTopSection(context, ref, vm, state),
  139. ),
  140. pinned: currentPageIdx !=0? true:false,
  141. ),
  142. // top 组件,转换为 Sliver 组件
  143. // SliverToBoxAdapter(
  144. // child: _buildTopSection(context, ref, vm, state),
  145. // ),
  146. ];
  147. },
  148. body: Column(
  149. mainAxisSize: MainAxisSize.max,
  150. children: [
  151. Expanded(
  152. child: AutoTabsRouter.pageView(
  153. key: tabsRouterKey,
  154. routes: const [
  155. HomeServicePageRoute(),
  156. InProgressPageRoute(),
  157. HistoryPageRoute(),
  158. ],
  159. builder: (context, child, pageController) {
  160. final tabsRouter = AutoTabsRouter.of(context);
  161. return Column(
  162. children: [
  163. Expanded(child: child),
  164. ],
  165. );
  166. },
  167. )
  168. )
  169. ]
  170. )
  171. )
  172. );
  173. }
  174. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, state) {
  175. final topSectionsData = vm.topSectionsData;
  176. final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  177. return Container(
  178. color: context.appColors.whiteBG,
  179. child: Center(
  180. child: Row(
  181. mainAxisSize: MainAxisSize.max,
  182. mainAxisAlignment: MainAxisAlignment.center,
  183. crossAxisAlignment: CrossAxisAlignment.center,
  184. children: List.generate(topSectionsData.length, (index) {
  185. final item = topSectionsData[index];
  186. return Column(
  187. children: [
  188. Container(
  189. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  190. height: 70,
  191. decoration: BoxDecoration(
  192. shape: BoxShape.circle, // 设置为圆形
  193. color: ColorUtils.string2Color("#F0F8FF"),
  194. boxShadow: index == currentPageIdx ? [
  195. BoxShadow(
  196. color: DarkThemeUtil.multiColors(context, context.appColors.tabLightBlueShadow, darkColor: AppColorsTheme.colorPrimary,)??context.appColors.tabLightBlueShadow, // 设置阴影颜色
  197. blurRadius: 5, // 设置模糊半径
  198. spreadRadius: 0.05, // 控制阴影扩散
  199. offset: const Offset(0, 2), // 设置阴影偏移量
  200. ),] : [],// 未选中时无阴影,
  201. ),
  202. child: MyAssetImage(
  203. item['icon'],
  204. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  205. // width: 70,
  206. height: 70,
  207. ).onTap(() {
  208. vm.handlerSwitchPageView(context, index);
  209. },
  210. type: ClickType.throttle,
  211. ),
  212. ),
  213. MyTextView(
  214. item['title'],
  215. fontSize: 15,
  216. marginTop: 8,
  217. textColor: index == currentPageIdx ? DarkThemeUtil.multiColors(context, context.appColors.textPrimary, darkColor: AppColorsTheme.colorPrimary): context.appColors.textBlack,
  218. textAlign: TextAlign.center,
  219. isFontMedium: true,
  220. ),
  221. ],
  222. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  223. }),
  224. ),
  225. ),
  226. );
  227. }
  228. }