repair_page.dart 8.9 KB

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