zodiac_love_intimacy_page.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import 'package:collection/collection.dart';
  2. import 'package:flutter/Material.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:get/get.dart';
  5. import 'package:keyboard/base/base_page.dart';
  6. import 'package:keyboard/data/repository/account_repository.dart';
  7. import 'package:keyboard/dialog/login/login_dialog.dart';
  8. import 'package:keyboard/module/profile/profile_page.dart';
  9. import 'package:keyboard/module/zodiac_love_intimacy/tody/zodiac_love_today_view.dart';
  10. import 'package:keyboard/module/zodiac_love_intimacy/zodiac_love_intimacy_controller.dart';
  11. import 'package:keyboard/resource/colors.gen.dart';
  12. import 'package:keyboard/router/app_page_arguments.dart';
  13. import 'package:keyboard/utils/toast_util.dart';
  14. import 'package:lottie/lottie.dart';
  15. import 'package:nested_scroll_views/material.dart';
  16. import '../../data/repository/keyboard_repository.dart';
  17. import '../../di/get_it.dart';
  18. import '../../resource/assets.gen.dart';
  19. import '../../resource/string.gen.dart';
  20. import '../../router/app_pages.dart';
  21. import '../../utils/age_zodiac_sign_util.dart';
  22. import '../../widget/avatar/avatar_image_widget.dart';
  23. import '../../widget/status_bar_placeholder_widget.dart';
  24. import '../../widget/top_bar.dart';
  25. import '../user_profile/user_profile_page.dart';
  26. import 'enums/zodiac_love_intimacy_tab.dart';
  27. import 'future_week/zodiac_love_future_week_view.dart';
  28. /// 星座恋爱分析Tab页
  29. class ZodiacLoveIntimacyPage extends BasePage<ZodiacLoveIntimacyController> {
  30. const ZodiacLoveIntimacyPage({super.key});
  31. static start({ZodiacLoveIntimacyTab tab = ZodiacLoveIntimacyTab.today}) {
  32. var accountRepository = getIt.get<AccountRepository>();
  33. var keyboardRepository = getIt.get<KeyboardRepository>();
  34. // 未登录,要求先登录
  35. if (!accountRepository.isLogin.value) {
  36. ToastUtil.show(StringName.accountNoLogin);
  37. LoginDialog.show();
  38. return;
  39. }
  40. // 如果用户未设置生日,则要求先设置生日,才能跳转
  41. if (accountRepository.userInfo.value?.birthday == null) {
  42. ToastUtil.show(StringName.userNotSetBirthdayTip);
  43. UserProfilePage.start();
  44. return;
  45. }
  46. // 未选择档案,要求先创建或选择档案
  47. if (keyboardRepository.chooseKeyboardInfo.value == null) {
  48. ToastUtil.show(StringName.userNotCreateProfile);
  49. ProfilePage.start();
  50. return;
  51. }
  52. var args = {AppPageArguments.index: tab.tabIndex};
  53. Get.toNamed(RoutePath.zodiacLoveIntimacy, arguments: args);
  54. }
  55. @override
  56. bool immersive() {
  57. return true;
  58. }
  59. @override
  60. bool statusBarDarkFont() {
  61. return false;
  62. }
  63. @override
  64. backgroundColor() {
  65. return Colors.transparent;
  66. }
  67. @override
  68. Widget buildBody(BuildContext context) {
  69. return Scaffold(
  70. backgroundColor: backgroundColor(),
  71. body: Stack(
  72. children: [
  73. // 背景图
  74. Positioned.fill(child: _buildBackgroundImage()),
  75. // 内容,填充剩余部分
  76. Positioned.fill(
  77. top: 0,
  78. left: 0,
  79. right: 0,
  80. bottom: 0,
  81. child: _buildContent(context),
  82. ),
  83. // 状态栏和标题栏
  84. Positioned(
  85. left: 0,
  86. top: 0,
  87. right: 0,
  88. child: Column(
  89. mainAxisSize: MainAxisSize.min,
  90. children: [StatusBarPlaceholderWidget(), _buildTopBar()],
  91. ),
  92. ),
  93. ],
  94. ),
  95. );
  96. }
  97. /// 背景图
  98. Widget _buildBackgroundImage() {
  99. return Container(
  100. child: Assets.images.bgZodiacLoveIntimacy.image(
  101. fit: BoxFit.cover,
  102. width: double.infinity,
  103. height: double.infinity,
  104. ),
  105. );
  106. }
  107. /// 顶部栏
  108. Widget _buildTopBar() {
  109. return TopBar(
  110. leftWidget: GestureDetector(
  111. onTap: controller.clickBack,
  112. child: Assets.images.iconWhiteBackArrow.image(
  113. width: 24.w,
  114. height: 24.h,
  115. ),
  116. ),
  117. centerWidget: Text(
  118. StringName.zodiacLoveIntimacy,
  119. style: TextStyle(
  120. color: ColorName.white,
  121. fontSize: 17.sp,
  122. fontWeight: FontWeight.w500,
  123. ),
  124. ),
  125. );
  126. }
  127. /// 指示器
  128. Widget _buildTabBar() {
  129. return Obx(() {
  130. return PreferredSize(
  131. preferredSize: Size.fromHeight(45.h),
  132. child: Stack(
  133. children: [
  134. Container(
  135. height: 41.h,
  136. margin: EdgeInsets.only(top: 6.h),
  137. // 顶部2边圆角
  138. decoration: BoxDecoration(
  139. color: Color(0xFFEBE7FD),
  140. borderRadius: BorderRadius.only(
  141. topLeft: Radius.circular(45.r),
  142. topRight: Radius.circular(45.r),
  143. ),
  144. ),
  145. ),
  146. // Tab
  147. TabBar(
  148. // 只有2个Tab,均分宽度,所以不可以滚动
  149. isScrollable: false,
  150. // 去除底部的黑线
  151. dividerHeight: 0,
  152. // 去掉自带的指示器
  153. indicator: BoxDecoration(),
  154. // 对齐方式,填充式对齐
  155. tabAlignment: TabAlignment.fill,
  156. // 指示器的高度设置为0,才能完全隐藏,否则还是会有一条线的高度
  157. indicatorWeight: 0,
  158. // 移除左右边距
  159. padding: EdgeInsets.zero,
  160. // 移除指示器与标签的间距
  161. indicatorPadding: EdgeInsets.zero,
  162. // 移除标签内部边距
  163. labelPadding: EdgeInsets.zero,
  164. // 配置Tab数据
  165. tabs:
  166. controller.tabBarList.mapIndexed((
  167. int index,
  168. ZodiacLoveIntimacyTab tab,
  169. ) {
  170. bool isSelected = controller.currentTabIndex.value == index;
  171. return _buildTab(tab, isSelected);
  172. }).toList(),
  173. controller: controller.tabController,
  174. onTap: (index) {
  175. controller.handleTabChange(index);
  176. },
  177. ),
  178. ],
  179. ),
  180. );
  181. });
  182. }
  183. /// 每个Tab
  184. Tab _buildTab(ZodiacLoveIntimacyTab tab, bool isSelected) {
  185. String tabName = tab.getTabName();
  186. TextStyle tabTextStyle;
  187. if (isSelected) {
  188. // 选中时的颜色
  189. tabTextStyle = TextStyle(
  190. fontSize: 14.sp,
  191. fontWeight: FontWeight.w700,
  192. color: ColorName.black80,
  193. );
  194. } else {
  195. // 未选中时的颜色
  196. tabTextStyle = TextStyle(
  197. fontSize: 14.sp,
  198. fontWeight: FontWeight.w600,
  199. color: ColorName.black60,
  200. );
  201. }
  202. return Tab(
  203. child: Container(
  204. width: double.infinity,
  205. height: double.infinity,
  206. // 未选中时,距离顶部有一定距离,选中时则撑满TabBar
  207. margin: EdgeInsets.only(top: isSelected ? 0 : 6.h),
  208. decoration: BoxDecoration(
  209. image:
  210. // 选中时,才有背景图
  211. isSelected
  212. ? DecorationImage(
  213. image: tab.getTabSelectedBg(),
  214. fit: BoxFit.fill,
  215. )
  216. : null,
  217. ),
  218. child: Center(
  219. child: Container(
  220. margin: EdgeInsets.only(top: 5.h),
  221. child: Text(tabName, style: tabTextStyle),
  222. ),
  223. ),
  224. ),
  225. );
  226. }
  227. /// 内容
  228. Widget _buildContent(BuildContext context) {
  229. return Column(
  230. children: [
  231. // 顶部基础信息
  232. _buildTopBasicInfoHeader(),
  233. // TabBar
  234. _buildTabBar(),
  235. // PageView
  236. Expanded(child: _buildPage()),
  237. ],
  238. );
  239. }
  240. /// 顶部基础信息视图
  241. Widget _buildTopBasicInfoHeader() {
  242. return Column(
  243. mainAxisAlignment: MainAxisAlignment.center,
  244. crossAxisAlignment: CrossAxisAlignment.center,
  245. children: [
  246. SizedBox(height: 70.w),
  247. // 头像布局
  248. _buildAvatarLayout(),
  249. SizedBox(height: 14.w),
  250. // 星座梗语与解读
  251. _buildZodiacDesc(),
  252. SizedBox(height: 16.w),
  253. ],
  254. );
  255. }
  256. /// 头像布局
  257. Widget _buildAvatarLayout() {
  258. return Obx(() {
  259. String? myAvatar =
  260. controller.zodiacLoveIntimacyLoveInfoResponse.value?.imageUrl ?? "";
  261. if (myAvatar.isEmpty) {
  262. myAvatar = controller.userInfo.value?.imageUrl ?? "";
  263. }
  264. return Row(
  265. mainAxisAlignment: MainAxisAlignment.center,
  266. children: [
  267. // 我的头像
  268. _buildAvatarAndZodiac(
  269. imageUrl: myAvatar,
  270. zodiac: controller.myZodiacInfo,
  271. ),
  272. // 爱心动画
  273. Lottie.asset(
  274. Assets.anim.animNewUserData,
  275. repeat: true,
  276. width: 131.w,
  277. fit: BoxFit.contain,
  278. ),
  279. // Ta的头像
  280. _buildAvatarAndZodiac(
  281. imageUrl:
  282. controller
  283. .zodiacLoveIntimacyLoveInfoResponse
  284. .value
  285. ?.targetImageUrl,
  286. zodiac: controller.taZodiacInfo,
  287. ),
  288. ],
  289. );
  290. });
  291. }
  292. /// 头像和星座
  293. Widget _buildAvatarAndZodiac({
  294. required String? imageUrl,
  295. required Zodiac? zodiac,
  296. }) {
  297. return Column(
  298. children: [
  299. SizedBox(height: 20.w),
  300. // 头像
  301. CircleAvatarWidget(
  302. image: Assets.images.iconKeyboardDefaultAvatar.provider(),
  303. imageUrl: imageUrl,
  304. size: 88.w,
  305. borderColor: Colors.white,
  306. borderWidth: 2.r,
  307. placeholder: (_, __) => const SizedBox.shrink(),
  308. ),
  309. SizedBox(height: 14.w),
  310. // 星座
  311. Builder(
  312. builder: (context) {
  313. if (zodiac != null) {
  314. return Row(
  315. children: [
  316. zodiac.image.image(width: 14.w, height: 14.w),
  317. SizedBox(width: 4.w),
  318. Text(
  319. zodiac.name,
  320. style: TextStyle(
  321. color: Colors.white,
  322. fontSize: 15.sp,
  323. fontWeight: FontWeight.w700,
  324. ),
  325. ),
  326. ],
  327. );
  328. } else {
  329. return const SizedBox.shrink();
  330. }
  331. },
  332. ),
  333. ],
  334. );
  335. }
  336. /// 星座梗语与解读
  337. Widget _buildZodiacDesc() {
  338. return Obx(() {
  339. var info = controller.zodiacLoveIntimacyLoveInfoResponse.value;
  340. var meme = info?.meme ?? "";
  341. var explain = info?.explain ?? "";
  342. // 无数据,不显示
  343. if (meme.isEmpty && explain.isEmpty) {
  344. return SizedBox(height: 67.h);
  345. }
  346. return Stack(
  347. alignment: Alignment.center,
  348. children: [
  349. Positioned(
  350. // IntrinsicWidth,包裹内容,宽度不撑大到屏幕宽度
  351. child: IntrinsicWidth(
  352. child: Container(
  353. alignment: Alignment.center,
  354. padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 42.w),
  355. // 圆角背景
  356. decoration: ShapeDecoration(
  357. color: const Color(0x29B80081),
  358. shape: RoundedRectangleBorder(
  359. borderRadius: BorderRadius.circular(16.r),
  360. ),
  361. ),
  362. child: Column(
  363. mainAxisSize: MainAxisSize.min,
  364. children: [
  365. // 梗语标题
  366. Visibility(
  367. visible: meme.isNotEmpty,
  368. child: Container(
  369. margin: EdgeInsets.only(bottom: 2.h),
  370. child: Text(
  371. meme,
  372. textAlign: TextAlign.center,
  373. style: TextStyle(
  374. color: ColorName.white,
  375. fontSize: 12.sp,
  376. fontWeight: FontWeight.w500,
  377. ),
  378. ),
  379. ),
  380. ),
  381. // 星座梗语
  382. Visibility(
  383. visible: explain.isNotEmpty,
  384. child: Text(
  385. explain,
  386. textAlign: TextAlign.center,
  387. style: TextStyle(
  388. color: ColorName.white,
  389. fontSize: 12.sp,
  390. fontWeight: FontWeight.w500,
  391. ),
  392. ),
  393. ),
  394. ],
  395. ),
  396. ),
  397. ),
  398. ),
  399. // 左上角的逗号
  400. Positioned(
  401. top: 5.w,
  402. left: 8.w,
  403. child: Assets.images.iconNewUserZodiacLeft.image(
  404. width: 13.w,
  405. height: 13.w,
  406. fit: BoxFit.contain,
  407. ),
  408. ),
  409. // 右下角的逗号
  410. Positioned(
  411. right: 8.w,
  412. bottom: 5.w,
  413. child: Assets.images.iconNewUserZodiacRight.image(
  414. width: 13.w,
  415. height: 13.w,
  416. fit: BoxFit.contain,
  417. ),
  418. ),
  419. ],
  420. );
  421. });
  422. }
  423. /// PageView
  424. Widget _buildPage() {
  425. return Obx(() {
  426. return NestedPageView(
  427. controller: controller.pageController,
  428. // 保持页面缓存
  429. wantKeepAlive: true,
  430. // 是否禁止滑动切换
  431. physics:
  432. controller.isPageViewSwipeEnabled.value
  433. ? ScrollPhysics()
  434. : NeverScrollableScrollPhysics(),
  435. onPageChanged: (index) {
  436. controller.handlePageChange(index);
  437. },
  438. children: [
  439. // 今日Tab
  440. ZodiacLoveTodayView(),
  441. // 未来一周Tab
  442. ZodiacLoveFutureWeekView(),
  443. ],
  444. );
  445. });
  446. }
  447. }