services_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import 'package:cpt_services/modules/services/services_pageview_idx_data.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:auto_route/auto_route.dart';
  5. import 'package:flutter/rendering.dart';
  6. import 'package:flutter_hooks/flutter_hooks.dart';
  7. import 'package:hooks_riverpod/hooks_riverpod.dart';
  8. import 'package:plugin_basic/provider/user_config/user_config_service.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:shared/utils/color_utils.dart';
  11. import 'package:shared/utils/log_utils.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 'services_vm.dart';
  20. final tabsRouterKey = GlobalKey<AutoTabsRouterState>();
  21. final GlobalKey<ExtendedNestedScrollViewState> extendedNestedScrollViewKey =
  22. GlobalKey<ExtendedNestedScrollViewState>();
  23. @RoutePage()
  24. class ServicesPage extends HookConsumerWidget with WidgetsBindingObserver {
  25. ServicesPage({Key? key}) : super(key: key);
  26. //启动当前页面
  27. static void startInstance({BuildContext? context}) {
  28. if (context != null) {
  29. context.router.push( ServicesPageRoute());
  30. } else {
  31. appRouter.push( ServicesPageRoute());
  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(servicesVmProvider.notifier);
  78. final state = ref.watch(servicesVmProvider);
  79. useEffect(() {
  80. // 监听窗口
  81. WidgetsBinding.instance.addObserver(this);
  82. }, []);
  83. useEffect((){
  84. Log.d("ServicesPage initState");
  85. // 延迟监听
  86. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  87. if(tabsRouterKey.currentState?.controller != null){
  88. tabsRouterKey.currentState?.controller?.addListener((){
  89. vm.tabsRouterChange();
  90. });
  91. }
  92. });
  93. return (){
  94. Log.d("ServicesPage dispose");
  95. WidgetsBinding.instance.removeObserver(this);
  96. tabsRouterKey.currentState?.controller?.removeListener(vm.tabsRouterChange);
  97. };
  98. },[]);
  99. return Scaffold(
  100. appBar: MyAppBar.searchAppBar(
  101. context,
  102. value: vm.getCurrentQueryParams('keyword'),
  103. actions: [
  104. const MyAssetImage(
  105. Assets.serviceServiceIcon1,
  106. width: 21.5,
  107. height: 21.5,
  108. ).onTap((){
  109. vm.handlerClickNavbarLikeBtn(context);
  110. }),
  111. const SizedBox(width: 15),
  112. ],
  113. backgroundColor: context.appColors.backgroundWhite,
  114. onSearch: (value) {
  115. vm.handlerSearch(value);
  116. }
  117. ),
  118. backgroundColor: context.appColors.backgroundDefault,
  119. body: ExtendedNestedScrollView(
  120. key: extendedNestedScrollViewKey,
  121. onlyOneScrollInBody: true,
  122. headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  123. return [
  124. // SliverPersistentHeader(
  125. // delegate: CustomSliverPersistentHeaderDelegate(
  126. // maxHeight: 180,
  127. // minHeight: 180,
  128. // child: _buildTopSection(context, ref, vm, state),
  129. // ),
  130. // pinned: false,
  131. // ),
  132. // top 组件,转换为 Sliver 组件
  133. SliverToBoxAdapter(
  134. child: _buildTopSection(context, ref, vm, state),
  135. ),
  136. ];
  137. },
  138. body: Column(
  139. mainAxisSize: MainAxisSize.max,
  140. children: [
  141. Expanded(
  142. child: AutoTabsRouter.pageView(
  143. key: tabsRouterKey,
  144. routes: const [
  145. HomeServicePageRoute(),
  146. ],
  147. builder: (context, child, pageController) {
  148. final tabsRouter = AutoTabsRouter.of(context);
  149. return Column(
  150. children: [
  151. Expanded(
  152. child: child
  153. ),
  154. ],
  155. );
  156. },
  157. )
  158. )
  159. ]
  160. )
  161. )
  162. );
  163. }
  164. Widget _buildTopSection(BuildContext context, WidgetRef ref, vm, state) {
  165. final topSectionsData = vm.topSectionsData;
  166. final currentPageIdx = tabsRouterKey.currentState?.controller?.activeIndex ?? 0;
  167. return Container(
  168. color: context.appColors.whiteBG,
  169. padding: const EdgeInsets.only(top: 23, bottom: 30),
  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. },
  200. type: ClickType.throttle,
  201. ),
  202. ),
  203. SizedBox.fromSize(size: const Size(0, 9)),
  204. MyTextView(
  205. item['title'],
  206. fontSize: 15,
  207. textColor: index == currentPageIdx ? ColorUtils.string2Color('#4161D0'): context.appColors.textBlack,
  208. textAlign: TextAlign.center,
  209. isFontMedium: true,
  210. ),
  211. ],
  212. ).marginOnly(left: 18, right: 18, top: 10, bottom: 10);
  213. }),
  214. ),
  215. ),
  216. );
  217. }
  218. Widget _buildNewsFeedPost(BuildContext context, WidgetRef ref, vm, state){
  219. final userConfig = UserConfigService.getState(ref: ref);
  220. return Container(
  221. height: 65.5,
  222. width: double.infinity,
  223. padding: const EdgeInsets.only(left: 20, right: 20),
  224. color: Colors.white,
  225. child: Row(
  226. children: [
  227. // const MyAssetImage(Assets.servicesNewsFeed, width: 45,height: 45,),
  228. MyLoadImage(
  229. userConfig.user?.avatar,
  230. width: 45,
  231. height: 45,
  232. isCircle: true,
  233. fit: BoxFit.fill,
  234. ),
  235. Expanded(
  236. child: MyTextView(
  237. "What’s on your mind?",
  238. textColor: context.appColors.textBlack,
  239. fontSize: 15,
  240. marginLeft: 15,
  241. alignment: Alignment.centerLeft,
  242. textAlign: TextAlign.left,
  243. backgroundColor: ColorUtils.string2Color('#ffffff'),
  244. maxLines: 1,
  245. isFontMedium: true,
  246. ),
  247. ),
  248. const MyAssetImage(
  249. Assets.serviceServiceIcon1,
  250. width: 21,
  251. height: 16.5,
  252. ),
  253. ],
  254. ).onTap((){
  255. vm.handlerGotoNewsfeedPost(context);
  256. }),
  257. );
  258. }
  259. Widget _buildGarageSalePost(BuildContext context, WidgetRef ref, vm, state){
  260. final userConfig = UserConfigService.getState(ref: ref);
  261. return Container(
  262. height: 65.5,
  263. width: double.infinity,
  264. padding: const EdgeInsets.only(left: 20, right: 20),
  265. color: Colors.white,
  266. child: Row(
  267. children: [
  268. MyLoadImage(
  269. userConfig.user?.avatar,
  270. width: 45,
  271. height: 45,
  272. isCircle: true,
  273. fit: BoxFit.fill,
  274. ),
  275. Expanded(
  276. child: MyTextView(
  277. "Sell Item",
  278. textColor: context.appColors.textBlack,
  279. fontSize: 15,
  280. marginLeft: 15,
  281. alignment: Alignment.centerLeft,
  282. textAlign: TextAlign.left,
  283. backgroundColor: ColorUtils.string2Color('#ffffff'),
  284. maxLines: 1,
  285. isFontMedium: true,
  286. ),
  287. ),
  288. const MyAssetImage(
  289. Assets.serviceServiceIcon1,
  290. width: 21,
  291. height: 16.5,
  292. ),
  293. ],
  294. ).onTap((){
  295. vm.handlerGotoGaragePost(context);
  296. }),
  297. );
  298. }
  299. }