repair_page.dart 9.3 KB

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