repair_page.dart 8.9 KB

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