repair_page.dart 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. RepairPage({Key? key}) : super(key: key);
  26. //启动当前页面
  27. static void startInstance({BuildContext? context}) {
  28. if (context != null) {
  29. context.router.push( RepairPageRoute());
  30. } else {
  31. appRouter.push( RepairPageRoute());
  32. }
  33. }
  34. bool _isKeyboardVisible = false;
  35. void handlerNestedScrollViewScroll({double? outerOffset, double? innerOffset, bool? isOuterScrollAnimated=false, bool? isInnerScrollAnimated=false}){
  36. if(outerOffset !=null){
  37. if(isOuterScrollAnimated!){
  38. extendedNestedScrollViewKey.currentState?.outerController.animateTo(
  39. outerOffset,
  40. duration: const Duration(seconds: 1),
  41. curve: Curves.easeIn,
  42. );
  43. }else {
  44. extendedNestedScrollViewKey.currentState?.outerController.jumpTo(
  45. outerOffset,
  46. );
  47. }
  48. }
  49. if(innerOffset !=null){
  50. extendedNestedScrollViewKey.currentState?.innerPositions.forEach((position) {
  51. if(isInnerScrollAnimated!){
  52. position.animateTo(innerOffset,
  53. duration: Duration(seconds: 1), curve: Curves.easeIn);
  54. }else {
  55. position.jumpTo(innerOffset);
  56. }
  57. });
  58. }
  59. }
  60. @override
  61. void didChangeMetrics() {
  62. final bottomInset = WidgetsBinding.instance.window.viewInsets.bottom;
  63. final newValue = bottomInset > 0.0;
  64. if (_isKeyboardVisible != newValue) {
  65. _isKeyboardVisible = newValue;
  66. if (_isKeyboardVisible) {
  67. handlerNestedScrollViewScroll(innerOffset: 0.0,);
  68. print("键盘已显示");
  69. } else {
  70. WidgetsBinding.instance.removeObserver(this);
  71. print("键盘已隐藏");
  72. }
  73. }
  74. }
  75. @override
  76. Widget build(BuildContext context, WidgetRef ref) {
  77. final vm = ref.read(repairVmProvider.notifier);
  78. final state = ref.watch(repairVmProvider);
  79. final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  80. useEffect(() {
  81. // 监听窗口
  82. WidgetsBinding.instance.addObserver(this);
  83. }, []);
  84. useEffect((){
  85. Log.d("RepairPage initState");
  86. // 延迟监听
  87. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  88. if(tabsRouterKey.currentState?.controller != null){
  89. tabsRouterKey.currentState?.controller?.addListener((){
  90. vm.tabsRouterChange();
  91. });
  92. }
  93. });
  94. return (){
  95. Log.d("RepairPage dispose");
  96. WidgetsBinding.instance.removeObserver(this);
  97. tabsRouterKey.currentState?.controller?.removeListener(vm.tabsRouterChange);
  98. };
  99. },[]);
  100. return Scaffold(
  101. appBar: MyAppBar.searchAppBar(
  102. context,
  103. value: vm.getCurrentQueryParams('keyword'),
  104. actions: [
  105. const MyAssetImage(
  106. Assets.serviceServiceScoreYes,
  107. width: 21.5,
  108. height: 21.5,
  109. ).onTap((){
  110. vm.handlerClickNavbarLikeBtn(context);
  111. }),
  112. const SizedBox(width: 15),
  113. ],
  114. backgroundColor: context.appColors.backgroundWhite,
  115. onSearch: (value) {
  116. vm.handlerSearch(value);
  117. }
  118. ),
  119. backgroundColor: context.appColors.backgroundDefault,
  120. body: ExtendedNestedScrollView(
  121. key: extendedNestedScrollViewKey,
  122. onlyOneScrollInBody: true,
  123. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  124. return [
  125. SliverPersistentHeader(
  126. delegate: CustomSliverPersistentHeaderDelegate(
  127. maxHeight: 130,
  128. minHeight: 130,
  129. child: _buildTopSection(context, ref, vm, state),
  130. ),
  131. pinned: currentPageIdx !=0? true:false,
  132. ),
  133. // top 组件,转换为 Sliver 组件
  134. // SliverToBoxAdapter(
  135. // child: _buildTopSection(context, ref, vm, state),
  136. // ),
  137. ];
  138. },
  139. body: Column(
  140. mainAxisSize: MainAxisSize.max,
  141. children: [
  142. Expanded(
  143. child: AutoTabsRouter.pageView(
  144. key: tabsRouterKey,
  145. routes: const [
  146. RepairHomeServicePageRoute(),
  147. RepairInProgressPageRoute(),
  148. RepairHistoryPageRoute(),
  149. ],
  150. builder: (context, child, pageController) {
  151. final tabsRouter = AutoTabsRouter.of(context);
  152. return Column(
  153. children: [
  154. Expanded(child: child),
  155. ],
  156. );
  157. },
  158. )
  159. )
  160. ]
  161. )
  162. )
  163. );
  164. }
  165. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, state) {
  166. final topSectionsData = vm.topSectionsData;
  167. final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  168. return Container(
  169. color: context.appColors.whiteBG,
  170. child: Center(
  171. child: Row(
  172. mainAxisSize: MainAxisSize.max,
  173. mainAxisAlignment: MainAxisAlignment.center,
  174. crossAxisAlignment: CrossAxisAlignment.center,
  175. children: List.generate(topSectionsData.length, (index) {
  176. final item = topSectionsData[index];
  177. return Column(
  178. children: [
  179. Container(
  180. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  181. height: 70,
  182. decoration: BoxDecoration(
  183. shape: BoxShape.circle, // 设置为圆形
  184. color: ColorUtils.string2Color("#F0F8FF"),
  185. boxShadow: index == currentPageIdx ? [
  186. BoxShadow(
  187. color: context.appColors.tabLightBlueShadow, // 设置阴影颜色
  188. blurRadius: 5, // 设置模糊半径
  189. spreadRadius: 0.05, // 控制阴影扩散
  190. offset: const Offset(0, 4), // 设置阴影偏移量
  191. ),] : [],// 未选中时无阴影,
  192. ),
  193. child: MyAssetImage(
  194. item['icon'],
  195. width: MediaQuery.of(context).size.width / topSectionsData.length - 36,
  196. // width: 70,
  197. height: 70,
  198. ).onTap(() {
  199. vm.handlerSwitchPageView(context, index);
  200. },
  201. type: ClickType.throttle,
  202. ),
  203. ),
  204. SizedBox.fromSize(size: const Size(0, 9)),
  205. MyTextView(
  206. item['title'],
  207. fontSize: 15,
  208. textColor: index == currentPageIdx ? ColorUtils.string2Color('#4161D0'): context.appColors.textBlack,
  209. textAlign: TextAlign.center,
  210. isFontMedium: true,
  211. ),
  212. ],
  213. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  214. }),
  215. ),
  216. ),
  217. );
  218. }
  219. }