services_page.dart 8.7 KB

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