repair_page.dart 9.0 KB

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