repair_page.dart 8.7 KB

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