home_page.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. import 'package:cpt_main/modules/home/home_state.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:cs_resources/generated/l10n.dart';
  4. import 'package:cs_resources/theme/app_colors_theme.dart';
  5. import 'package:cs_resources/theme/theme_config.dart';
  6. import 'package:domain/entity/home_list_entity.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter/services.dart';
  9. import 'package:flutter/src/widgets/framework.dart';
  10. import 'package:flutter_hooks/flutter_hooks.dart';
  11. import 'package:hooks_riverpod/hooks_riverpod.dart';
  12. import 'package:auto_route/auto_route.dart';
  13. import 'package:plugin_basic/provider/user_config/user_config_service.dart';
  14. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  15. import 'package:router/componentRouter/community_service.dart';
  16. import 'package:router/componentRouter/component_service_manager.dart';
  17. import 'package:widgets/ext/ex_widget.dart';
  18. import 'package:widgets/my_appbar.dart';
  19. import 'package:widgets/my_load_image.dart';
  20. import 'package:widgets/my_text_view.dart';
  21. import 'package:widgets/utils/dark_theme_util.dart';
  22. import 'package:widgets/widget_export.dart';
  23. import 'item_home_category.dart';
  24. import 'home_view_model.dart';
  25. import 'item_home_last_news.dart';
  26. import 'item_home_last_trans.dart';
  27. import 'item_home_manage_guide.dart';
  28. import 'item_home_property_news.dart';
  29. import 'latest_news/info/latest_news_info_screen.dart';
  30. import 'latest_news/internal/latest_news_internal_screen.dart';
  31. import 'latest_news/property/latest_news_property_screen.dart';
  32. import 'latest_news/publish/latest_news_publish_screen.dart';
  33. @RoutePage()
  34. class HomePage extends HookConsumerWidget {
  35. const HomePage({super.key});
  36. @override
  37. Widget build(BuildContext context, WidgetRef ref) {
  38. final viewModel = ref.read(homeViewModelProvider.notifier);
  39. final state = ref.watch(homeViewModelProvider);
  40. final bannerIndex = useState(0);
  41. useEffect(() {
  42. // 组件挂载时执行 - 执行接口请求
  43. Future.microtask(() => viewModel.fetchHomeIndex());
  44. return () {
  45. // 组件卸载时执行
  46. };
  47. }, []);
  48. return AnnotatedRegion<SystemUiOverlayStyle>(
  49. value: MediaQuery.of(context).platformBrightness == Brightness.dark
  50. ? ThemeConfig.systemUiOverlayStyleDarkTheme
  51. : ThemeConfig.systemUiOverlayStyleLightThemeWhite,
  52. child: Scaffold(
  53. backgroundColor: context.appColors.backgroundDefault,
  54. body: Stack(
  55. children: [
  56. // 固定顶部的背景图片
  57. const Positioned(
  58. top: 0,
  59. left: 0,
  60. right: 0,
  61. child: MyAssetImage(
  62. Assets.mainHomeTopImgBg,
  63. width: double.infinity,
  64. fit: BoxFit.cover,
  65. height: 325,
  66. ), // 替换为你的图片路径
  67. ),
  68. Positioned(
  69. top: 58,
  70. left: 0,
  71. right: 0,
  72. child: Row(
  73. crossAxisAlignment: CrossAxisAlignment.center,
  74. children: [
  75. MyTextView(
  76. _formatNowGreetingText(ref),
  77. marginLeft: 17,
  78. textColor: context.appColors.textBlack,
  79. isFontBold: true,
  80. fontSize: 20,
  81. ).expanded(),
  82. //通知角标
  83. Center(
  84. child: Stack(
  85. clipBehavior: Clip.none, // 不裁剪超出边界的内容
  86. alignment: Alignment.topLeft,
  87. children: <Widget>[
  88. // 通知图标
  89. MyAssetImage(
  90. Assets.mainHomeNotificationIcon,
  91. width: 17,
  92. height: 19,
  93. color: context.appColors.imageDarkModelWhite,
  94. ),
  95. //未读消息
  96. Positioned(
  97. left: 0,
  98. top: 0,
  99. child: Transform.translate(
  100. offset: const Offset(-10, -5),
  101. child: MyTextView(
  102. "99",
  103. boxWidth: 20.0,
  104. textColor: Colors.white,
  105. fontSize: 10,
  106. isFontLight: true,
  107. backgroundColor: context.appColors.redDefault,
  108. cornerRadius: 8,
  109. paddingTop: 2,
  110. paddingBottom: 2,
  111. textAlign: TextAlign.center,
  112. ),
  113. ),
  114. ),
  115. ],
  116. ).onTap(viewModel.gotoNotificationPage))
  117. .marginOnly(right: 17),
  118. ],
  119. ), // 替换为你的图片路径
  120. ),
  121. Positioned(
  122. top: 100,
  123. left: 0,
  124. right: 0,
  125. bottom: 0,
  126. child: EasyRefresh(
  127. controller: viewModel.refreshController,
  128. onRefresh: viewModel.onRefresh,
  129. child: CustomScrollView(
  130. scrollDirection: Axis.vertical,
  131. slivers: [
  132. //支付与奖励
  133. _buildPaymentAndRewardsWidget(context, ref),
  134. //间距
  135. _buildSliverSpace(20),
  136. //九宫选项组 (固定)
  137. _buildCategoryWidget(context, ref),
  138. //轮播图片 (动态)
  139. _buildBannerImage(ref, bannerIndex),
  140. //最新的新闻(动态)
  141. _buildLastNews(context, ref),
  142. //最新的交易
  143. SliverToBoxAdapter(
  144. child: MyTextView(
  145. marginTop: 32,
  146. marginLeft: 15,
  147. marginBottom: 14,
  148. S.current.latest_transactions,
  149. textColor: context.appColors.textBlack,
  150. fontSize: 16,
  151. isFontMedium: true,
  152. ),
  153. ),
  154. //最新交易列表 (动态)
  155. _buildLastTransaction(context, state),
  156. //房产新闻
  157. SliverToBoxAdapter(
  158. child: Row(
  159. children: [
  160. MyTextView(
  161. marginTop: 30,
  162. marginLeft: 15,
  163. marginBottom: 14,
  164. S.current.property_news,
  165. textColor: context.appColors.textBlack,
  166. fontSize: 16,
  167. isFontMedium: true,
  168. ).expanded(),
  169. MyTextView(
  170. marginTop: 30,
  171. marginLeft: 15,
  172. marginRight: 15,
  173. marginBottom: 14,
  174. onClick: viewModel.gotoPropertyNewsPage,
  175. S.current.all,
  176. textColor: context.appColors.textBlack,
  177. fontSize: 15,
  178. isFontMedium: true,
  179. )
  180. ],
  181. ),
  182. ),
  183. //房产新闻列表 (动态)
  184. _buildPropertyNews(context, ref),
  185. //管理员介绍 (固定)
  186. _buildManagementGuides(context, ref),
  187. //间距
  188. _buildSliverSpace(15),
  189. ],
  190. ),
  191. ),
  192. ),
  193. ],
  194. ),
  195. ));
  196. }
  197. //顶部的支付与奖励的布局
  198. Widget _buildPaymentAndRewardsWidget(BuildContext context, WidgetRef ref) {
  199. final viewModel = ref.read(homeViewModelProvider.notifier);
  200. return SliverToBoxAdapter(
  201. child: Row(
  202. children: [
  203. Row(
  204. mainAxisSize: MainAxisSize.max,
  205. mainAxisAlignment: MainAxisAlignment.center,
  206. children: [
  207. const MyAssetImage(
  208. Assets.mainHomePaymentIcon,
  209. width: 24.5,
  210. height: 24.5,
  211. ).marginOnly(left: 10),
  212. MyTextView(
  213. S.current.payment,
  214. textColor: context.appColors.textBlack,
  215. fontSize: 14,
  216. isFontRegular: true,
  217. ).paddingOnly(left: 8, right: 8).expanded(),
  218. MyAssetImage(
  219. Assets.mainHomeMoreIcon,
  220. width: 6,
  221. height: 8.5,
  222. color: context.appColors.textBlack,
  223. ).marginOnly(right: 15),
  224. ],
  225. )
  226. .constrained(height: 46)
  227. .decorated(
  228. color: const Color(0xFFE0E3FF).withOpacity(0.5),
  229. borderRadius: BorderRadius.circular(9.0),
  230. )
  231. .onTap(viewModel.gotoPaymentPage)
  232. .expanded(),
  233. const SizedBox(width: 14.5),
  234. Row(
  235. mainAxisSize: MainAxisSize.max,
  236. mainAxisAlignment: MainAxisAlignment.center,
  237. children: [
  238. const MyAssetImage(
  239. Assets.mainHomeRewardsIcon,
  240. width: 24.5,
  241. height: 24.5,
  242. ).marginOnly(left: 10),
  243. MyTextView(
  244. S.current.rewards,
  245. textColor: context.appColors.textBlack,
  246. fontSize: 14,
  247. isFontRegular: true,
  248. ).paddingOnly(left: 8, right: 4).expanded(),
  249. MyTextView(
  250. "9568",
  251. textColor: context.appColors.textBlack,
  252. fontSize: 14,
  253. isFontBold: true,
  254. ).paddingOnly(left: 1, right: 4),
  255. MyAssetImage(
  256. Assets.mainHomeMoreIcon,
  257. width: 6,
  258. height: 8.5,
  259. color: context.appColors.textBlack,
  260. ).marginOnly(right: 8),
  261. ],
  262. )
  263. .constrained(height: 46)
  264. .decorated(
  265. color: const Color(0xFFE0E3FF).withOpacity(0.5),
  266. borderRadius: BorderRadius.circular(9.0),
  267. )
  268. .onTap(viewModel.gotoRewardsPage)
  269. .expanded(),
  270. ],
  271. ).paddingOnly(left: 15, right: 15, top: 10),
  272. );
  273. }
  274. Widget _buildSliverSpace(double size) {
  275. return SliverToBoxAdapter(
  276. child: SizedBox(height: size),
  277. );
  278. }
  279. //九宫格选项组
  280. Widget _buildCategoryWidget(BuildContext context, WidgetRef ref) {
  281. final viewModel = ref.read(homeViewModelProvider.notifier);
  282. final state = ref.watch(homeViewModelProvider);
  283. return SliverGrid(
  284. gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
  285. crossAxisCount: 3, // 三列
  286. mainAxisSpacing: 0.0, // 主轴(上下)的间距
  287. crossAxisSpacing: 0.0, // 交叉轴(左右)的间距
  288. ),
  289. delegate: SliverChildBuilderDelegate(
  290. (BuildContext context, int index) {
  291. return HomeCategoryItem(
  292. category: state.homeCategory[index],
  293. ).onTap(() {
  294. viewModel.switchCategory(index);
  295. }); // 生成每个网格项
  296. },
  297. childCount: state.homeCategory.length, // 总共的网格项数
  298. ),
  299. );
  300. }
  301. //Banner的布局
  302. Widget _buildBannerImage(WidgetRef ref, ValueNotifier<int> bannerIndex) {
  303. final viewModel = ref.read(homeViewModelProvider.notifier);
  304. final state = ref.watch(homeViewModelProvider);
  305. return SliverToBoxAdapter(
  306. child: Container(
  307. width: double.infinity,
  308. margin: const EdgeInsets.only(top: 30, left: 15, right: 15),
  309. child: state.homeIndex != null && state.homeIndex!.banners.isNotEmpty
  310. ? Stack(
  311. alignment: Alignment.bottomCenter, // 设置 Stack 的对齐方式为底部中心
  312. children: [
  313. CarouselSlider(
  314. options: CarouselOptions(
  315. aspectRatio: 345 / 152.5,
  316. viewportFraction: 1,
  317. initialPage: 0,
  318. enableInfiniteScroll: true,
  319. reverse: false,
  320. autoPlay: true,
  321. autoPlayInterval: const Duration(seconds: 5),
  322. autoPlayAnimationDuration: const Duration(milliseconds: 800),
  323. autoPlayCurve: Curves.fastOutSlowIn,
  324. enlargeCenterPage: true,
  325. scrollDirection: Axis.horizontal,
  326. onPageChanged: (index, reason) {
  327. bannerIndex.value = index;
  328. },
  329. ),
  330. items: state.homeIndex!.banners.map<Widget>((item) {
  331. return MyLoadImage(
  332. item.image,
  333. width: double.infinity,
  334. cornerRadius: 5,
  335. );
  336. }).toList(),
  337. ),
  338. Positioned(
  339. bottom: 10, // 距离底部 20 像素
  340. child: Row(
  341. mainAxisAlignment: MainAxisAlignment.center,
  342. children: state.homeIndex!.banners.map((item) {
  343. //难道就没有indexMap,要么就转换为Map的方式进行遍历Map
  344. int index = state.homeIndex!.banners.indexOf(item);
  345. return Container(
  346. width: 6.5,
  347. height: 6.5,
  348. margin: const EdgeInsets.symmetric(horizontal: 3.5),
  349. decoration: BoxDecoration(
  350. shape: BoxShape.circle,
  351. color: bannerIndex.value == index
  352. ? const Color(0x4D000000) // 选中状态颜色
  353. : const Color(0x1A000000), // 未选中状态颜色
  354. ),
  355. );
  356. }).toList(),
  357. ),
  358. ),
  359. ],
  360. )
  361. : AspectRatio(
  362. aspectRatio: 345 / 152.5,
  363. child: MyLoadImage(
  364. Assets.baseLibImageDefaultPlaceholder,
  365. width: double.infinity,
  366. cornerRadius: 5,
  367. ),
  368. ),
  369. ),
  370. );
  371. }
  372. //最新新闻
  373. Widget _buildLastNews(BuildContext context, WidgetRef ref) {
  374. final viewModel = ref.read(homeViewModelProvider.notifier);
  375. final state = ref.watch(homeViewModelProvider);
  376. return SliverToBoxAdapter(
  377. child: Column(
  378. crossAxisAlignment: CrossAxisAlignment.start,
  379. mainAxisAlignment: MainAxisAlignment.start,
  380. children: [
  381. MyTextView(
  382. S.current.latest_news,
  383. fontSize: 16,
  384. marginTop: 30,
  385. marginBottom: 14,
  386. isFontMedium: true,
  387. onClick: viewModel.gotoLastNewsPage,
  388. textColor: context.appColors.textBlack,
  389. ),
  390. Row(
  391. mainAxisAlignment: MainAxisAlignment.spaceAround, // 均匀排布
  392. children: List.generate(state.lastNews.length, (index) {
  393. return Expanded(
  394. // 使用 Expanded 使每个子项占据相同空间
  395. child: LastNewsItem(
  396. lastNews: state.lastNews[index],
  397. onItemTap: () {
  398. //根据不同的索引跳转到不同的PageView指定页面
  399. if (index == 0) {
  400. LatestNewsPropertyScreen.startInstance(context: context);
  401. } else if (index == 1) {
  402. LatestNewsInternalScreen.startInstance(context: context);
  403. } else if (index == 2) {
  404. LatestNewsInfoScreen.startInstance(context: context);
  405. } else if (index == 3) {
  406. LatestNewsPublishScreen.startInstance(context: context);
  407. }
  408. },
  409. ),
  410. );
  411. }),
  412. ),
  413. ],
  414. ).paddingOnly(left: 15, right: 15));
  415. }
  416. //最新的交易列表
  417. Widget _buildLastTransaction(BuildContext context, HomeState state) {
  418. return SliverPadding(
  419. padding: const EdgeInsets.symmetric(horizontal: 15),
  420. sliver: DecoratedSliver(
  421. decoration: BoxDecoration(
  422. color: context.appColors.whiteBG,
  423. borderRadius: BorderRadius.circular(5.0),
  424. boxShadow: [
  425. BoxShadow(
  426. color: const Color(0xFF656565).withOpacity(0.1),
  427. offset: const Offset(0, 1.5),
  428. blurRadius: 2.5,
  429. spreadRadius: 1.5,
  430. ),
  431. ],
  432. ),
  433. sliver: SliverPadding(
  434. padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 20),
  435. sliver: SliverList(
  436. delegate: SliverChildBuilderDelegate(
  437. (BuildContext context, int index) {
  438. return Padding(
  439. padding: const EdgeInsets.symmetric(vertical: 10),
  440. child: LastTransItem(
  441. lastTrans: state.homeIndex!.latestTransactions[index],
  442. ),
  443. );
  444. },
  445. childCount: state.homeIndex?.latestTransactions.length ?? 0,
  446. ),
  447. ),
  448. ),
  449. ),
  450. );
  451. }
  452. //房产新闻的列表
  453. Widget _buildPropertyNews(BuildContext context, WidgetRef ref) {
  454. final state = ref.watch(homeViewModelProvider);
  455. final propertyNewsList = state.homeIndex?.propertyNews ?? [];
  456. return SliverList(
  457. delegate: SliverChildListDelegate(
  458. [
  459. // 第一个水平滑动列表
  460. _buildPropertyNewsHorizontalList(propertyNewsList),
  461. ],
  462. ),
  463. );
  464. }
  465. Widget _buildPropertyNewsHorizontalList(List<HomeListPropertyNews> propertyNews) {
  466. return SingleChildScrollView(
  467. scrollDirection: Axis.horizontal,
  468. physics: const BouncingScrollPhysics(),
  469. clipBehavior: Clip.none,
  470. child: Row(
  471. children: propertyNews.map((news) {
  472. return PropertyNews(news: news); // 假设 PropertyNews 需要传入一个 news 参数
  473. }).toList(),
  474. ).marginOnly(left: 15, right: 15),
  475. );
  476. }
  477. //管理员介绍
  478. Widget _buildManagementGuides(BuildContext context, WidgetRef ref) {
  479. final viewModel = ref.read(homeViewModelProvider.notifier);
  480. final state = ref.watch(homeViewModelProvider);
  481. return SliverToBoxAdapter(
  482. child: Column(
  483. crossAxisAlignment: CrossAxisAlignment.start,
  484. mainAxisAlignment: MainAxisAlignment.start,
  485. children: [
  486. MyTextView(
  487. S.current.strata_management_guides,
  488. fontSize: 16,
  489. marginTop: 30,
  490. marginBottom: 14,
  491. onClick: viewModel.gotoManageGuidePage,
  492. isFontMedium: true,
  493. textColor: context.appColors.textBlack,
  494. ),
  495. SingleChildScrollView(
  496. scrollDirection: Axis.horizontal,
  497. physics: const BouncingScrollPhysics(),
  498. clipBehavior: Clip.none,
  499. child: Row(
  500. children: state.homeIndex?.strataManagementGuides == null
  501. ? [const SizedBox.shrink()]
  502. : List.generate(state.homeIndex!.strataManagementGuides.length, (index) {
  503. return ManageGuideItem(
  504. manageGuide: state.homeIndex!.strataManagementGuides[index],
  505. );
  506. }),
  507. ),
  508. )
  509. ],
  510. ).paddingOnly(left: 15, right: 15));
  511. }
  512. /// 根据现在的小时数,返回 早上好,中午好,下午好,晚上好 这四个字符串
  513. String _formatNowGreetingText(WidgetRef ref) {
  514. final now = DateTime.now();
  515. final hour = now.hour;
  516. String greeting;
  517. if (hour < 12) {
  518. greeting = S.current.good_morning(UserConfigService.getState(ref: ref).userName ?? "-");
  519. } else if (hour < 18) {
  520. greeting = S.current.good_afternoon(UserConfigService.getState(ref: ref).userName ?? "-");
  521. } else {
  522. greeting = S.current.good_evening(UserConfigService.getState(ref: ref).userName ?? "-");
  523. }
  524. return greeting;
  525. }
  526. }