home_view.dart 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. import 'package:carousel_slider/carousel_slider.dart';
  2. import 'package:flutter/material.dart' hide ConnectionState;
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:get/get.dart';
  5. import 'package:nomo/app/constants/iconfont/iconfont.dart';
  6. import 'package:nomo/app/data/sp/ix_sp.dart';
  7. import 'package:nomo/app/extensions/widget_extension.dart';
  8. import 'package:nomo/utils/misc.dart';
  9. import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
  10. import '../../../../config/theme/dark_theme_colors.dart';
  11. import '../../../../config/theme/light_theme_colors.dart';
  12. import '../../../../config/theme/theme_extensions/theme_extension.dart';
  13. import '../../../../config/translations/strings_enum.dart';
  14. import '../../../base/base_view.dart';
  15. import '../../../constants/assets.dart';
  16. import '../../../routes/app_pages.dart';
  17. import '../../../widgets/click_opacity.dart';
  18. import '../../../widgets/country_icon.dart';
  19. import '../../../widgets/ix_image.dart';
  20. import '../controllers/home_controller.dart';
  21. import '../widgets/connection_theme_button.dart';
  22. import '../widgets/menu_list.dart';
  23. class HomeView extends BaseView<HomeController> {
  24. const HomeView({super.key});
  25. @override
  26. bool get isPopScope => true;
  27. @override
  28. Widget buildContent(BuildContext context) {
  29. return _buildCustomScrollView();
  30. }
  31. Widget _buildCustomScrollView() {
  32. return SafeArea(
  33. child: Padding(
  34. padding: isDesktop
  35. ? EdgeInsets.all(14.w)
  36. : EdgeInsets.symmetric(horizontal: 14.w),
  37. child: Column(
  38. children: [
  39. _buildAppBar(),
  40. 20.verticalSpaceFromWidth,
  41. Expanded(
  42. child: LayoutBuilder(
  43. builder: (context, constraints) {
  44. // 确保 viewportHeight 不会为负数(键盘弹出时)
  45. final double viewportHeight = (constraints.maxHeight - 388.w)
  46. .clamp(0.0, double.infinity);
  47. return GestureDetector(
  48. behavior: HitTestBehavior.translucent,
  49. onTap: () {
  50. controller.collapseRecentLocations();
  51. },
  52. child: SmartRefresher(
  53. enablePullDown: true,
  54. enablePullUp: false,
  55. controller: controller.refreshController,
  56. onRefresh: controller.onRefresh,
  57. child: CustomScrollView(
  58. slivers: [
  59. // 1. 顶部和中间部分
  60. SliverList(
  61. delegate: SliverChildListDelegate([
  62. 20.verticalSpaceFromWidth,
  63. Stack(
  64. children: [
  65. Container(
  66. alignment: Alignment.center,
  67. margin: EdgeInsets.only(top: 138.w),
  68. child: _buildConnectionButton(),
  69. ),
  70. _buildLocationStack(),
  71. ],
  72. ),
  73. 20.verticalSpaceFromWidth,
  74. // // 第二部分:中间(内容可长可短)
  75. // Container(
  76. // color: Colors.green[100],
  77. // height: 400, // 修改这个高度来测试滚动效果
  78. // child: Center(child: Text("中间内容区域")),
  79. // ),
  80. ]),
  81. ),
  82. // 2. 底部部分:关键所在
  83. SliverFillRemaining(
  84. hasScrollBody: false,
  85. child: ConstrainedBox(
  86. constraints: BoxConstraints(
  87. minHeight: viewportHeight,
  88. ),
  89. child: Column(
  90. children: [
  91. Spacer(), // 自动撑开上方空间,将底部内容挤下去
  92. Padding(
  93. padding: EdgeInsets.symmetric(
  94. vertical: 14.w,
  95. ),
  96. child: Obx(() {
  97. if (controller.bannerList.isEmpty) {
  98. return SizedBox.shrink();
  99. }
  100. // 当前宽度 * 0.212
  101. final height =
  102. MediaQuery.of(context).size.width *
  103. 0.212;
  104. return Stack(
  105. children: [
  106. CarouselSlider(
  107. options: CarouselOptions(
  108. height: height,
  109. viewportFraction: 1.0,
  110. autoPlay:
  111. controller.bannerList.length >
  112. 1,
  113. autoPlayInterval: const Duration(
  114. seconds: 5,
  115. ),
  116. onPageChanged: (index, reason) {
  117. controller.currentBannerIndex =
  118. index;
  119. },
  120. ),
  121. items: controller.bannerList.map((
  122. banner,
  123. ) {
  124. return Builder(
  125. builder:
  126. (BuildContext context) {
  127. return GestureDetector(
  128. onTap: () => controller
  129. .onBannerTap(
  130. banner,
  131. ),
  132. child: IXImage(
  133. source:
  134. banner.img ?? '',
  135. width:
  136. double.infinity,
  137. height: height,
  138. sourceType:
  139. ImageSourceType
  140. .network,
  141. borderRadius: 14.r,
  142. ),
  143. ).withClickCursor(
  144. isDesktop,
  145. );
  146. },
  147. );
  148. }).toList(),
  149. ),
  150. if (controller.bannerList.length > 1)
  151. Positioned(
  152. bottom: 0,
  153. left: 0,
  154. right: 0,
  155. child: Row(
  156. mainAxisAlignment:
  157. MainAxisAlignment.center,
  158. children: controller.bannerList
  159. .asMap()
  160. .entries
  161. .map((entry) {
  162. return AnimatedContainer(
  163. duration:
  164. const Duration(
  165. milliseconds: 300,
  166. ),
  167. curve: Curves.easeInOut,
  168. width:
  169. controller
  170. .currentBannerIndex ==
  171. entry.key
  172. ? 16
  173. : 6,
  174. height: 6,
  175. margin:
  176. const EdgeInsets.symmetric(
  177. vertical: 8.0,
  178. horizontal: 2.0,
  179. ),
  180. decoration: BoxDecoration(
  181. borderRadius:
  182. BorderRadius.circular(
  183. 6,
  184. ),
  185. color: Colors.white
  186. .withValues(
  187. alpha:
  188. controller
  189. .currentBannerIndex ==
  190. entry
  191. .key
  192. ? 0.9
  193. : 0.4,
  194. ),
  195. ),
  196. );
  197. })
  198. .toList(),
  199. ),
  200. ),
  201. ],
  202. );
  203. }),
  204. ),
  205. MenuList(),
  206. if (controller.nineBannerList.isNotEmpty)
  207. 14.verticalSpaceFromWidth,
  208. ],
  209. ),
  210. ),
  211. ),
  212. ],
  213. ),
  214. ),
  215. );
  216. },
  217. ),
  218. ),
  219. ],
  220. ),
  221. ),
  222. );
  223. }
  224. Widget _buildAppBar() {
  225. return Row(
  226. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  227. children: [
  228. Obx(() {
  229. final bgColor = controller.apiController.userLevel == 3
  230. ? ReactiveTheme.isLightTheme
  231. ? LightThemeColors.homePremiumColor
  232. : DarkThemeColors.homePremiumColor
  233. : controller.apiController.userLevel == 9999
  234. ? ReactiveTheme.isLightTheme
  235. ? LightThemeColors.homeTestColor
  236. : DarkThemeColors.homeTestColor
  237. : ReactiveTheme.isLightTheme
  238. ? LightThemeColors.homeFreeColor
  239. : DarkThemeColors.homeFreeColor;
  240. // 获取 vipRemainNotice 配置
  241. final vipRemainNotice =
  242. (IXSP.getAppConfig()?.vipRemainNotice ?? 0) * 60;
  243. final remainTimeSeconds = controller.apiController.remainTimeSeconds;
  244. // 只有当剩余时间 > 0 且 < vipRemainNotice 时才显示提醒
  245. final showReminder =
  246. remainTimeSeconds > 0 && remainTimeSeconds < vipRemainNotice;
  247. return ClickOpacity(
  248. onTap: () => Get.toNamed(Routes.SUBSCRIPTION),
  249. child: Stack(
  250. children: [
  251. if (showReminder)
  252. Container(
  253. height: 28.w,
  254. padding: EdgeInsets.only(left: 32.w, right: 8.w),
  255. alignment: Alignment.center,
  256. margin: controller.apiController.userLevel == 3
  257. ? EdgeInsets.only(left: 64.w)
  258. : EdgeInsets.only(left: 36.w),
  259. decoration: BoxDecoration(
  260. borderRadius: BorderRadius.circular(100.r),
  261. color: bgColor,
  262. ),
  263. child: _buildReminder(),
  264. ),
  265. Obx(
  266. () => IXImage(
  267. source: controller.apiController.userLevel == 3
  268. ? controller.apiController.remainTimeSeconds > 0
  269. ? Assets.premium
  270. : Assets.premiumExpired
  271. : controller.apiController.userLevel == 9999
  272. ? Assets.test
  273. : Assets.free,
  274. width: controller.apiController.userLevel == 3
  275. ? 92.w
  276. : 64.w,
  277. height: 28.w,
  278. sourceType: ImageSourceType.asset,
  279. ),
  280. ),
  281. ],
  282. ),
  283. );
  284. }),
  285. ClickOpacity(
  286. child: Padding(
  287. padding: EdgeInsets.only(
  288. left: 10.w,
  289. right: 0.w,
  290. top: 10.w,
  291. bottom: 10.w,
  292. ),
  293. child: IXImage(
  294. source: Assets.menus,
  295. width: 26.w,
  296. height: 26.w,
  297. sourceType: ImageSourceType.asset,
  298. ),
  299. ),
  300. onTap: () {
  301. controller.collapseRecentLocations();
  302. Get.toNamed(Routes.SETTING);
  303. },
  304. ),
  305. ],
  306. );
  307. }
  308. Widget _buildReminder() {
  309. // return Obx(
  310. // () => Text(
  311. // controller.apiController.isGuest &&
  312. // !controller.apiController.isPremium &&
  313. // controller.apiController.remainTimeSeconds > 0
  314. // ? controller.apiController.remainTimeFormatted
  315. // : controller.coreController.timer,
  316. // style: TextStyle(
  317. // fontSize: 13.sp,
  318. // height: 1.5,
  319. // fontStyle: FontStyle.italic,
  320. // fontWeight: FontWeight.w500,
  321. // fontFeatures: [FontFeature.tabularFigures()],
  322. // color:
  323. // controller.apiController.userLevel == 3 ||
  324. // controller.apiController.userLevel == 9999
  325. // ? Get.reactiveTheme.textTheme.bodyLarge!.color
  326. // : Get.reactiveTheme.hintColor,
  327. // ),
  328. // ),
  329. // );
  330. return Obx(() {
  331. // 只有文案变化时才更新 UI
  332. final textColor = controller.apiController.userLevel == 3
  333. ? ReactiveTheme.isLightTheme
  334. ? LightThemeColors.homePremiumTextColor
  335. : DarkThemeColors.homePremiumTextColor
  336. : controller.apiController.userLevel == 9999
  337. ? ReactiveTheme.isLightTheme
  338. ? LightThemeColors.homeTestTextColor
  339. : DarkThemeColors.homeTestTextColor
  340. : ReactiveTheme.isLightTheme
  341. ? LightThemeColors.homeFreeTextColor
  342. : DarkThemeColors.homeFreeTextColor;
  343. return Text(
  344. controller.apiController.remainTimeFormatted,
  345. style: TextStyle(
  346. fontSize: 13.sp,
  347. height: 1.5,
  348. fontStyle: FontStyle.italic,
  349. fontWeight: FontWeight.w500,
  350. fontFeatures: [FontFeature.tabularFigures()],
  351. color: textColor,
  352. ),
  353. );
  354. });
  355. }
  356. Widget _buildConnectionButton() {
  357. return Obx(
  358. () => ConnectionThemeButton(
  359. state: controller.coreController.state,
  360. onTap: () {
  361. controller.collapseRecentLocations();
  362. controller.setDefaultAutoConnect();
  363. },
  364. ).withClickCursor(isDesktop),
  365. );
  366. }
  367. /// 构建位置堆叠效果(选中位置 + 最近位置)
  368. Widget _buildLocationStack() {
  369. return Obx(() {
  370. if (controller.selectedLocation == null) {
  371. return const SizedBox.shrink();
  372. }
  373. return Stack(
  374. children: [
  375. // 最近位置列表(背景层)
  376. if (controller.recentLocations.isNotEmpty)
  377. _buildRecentLocationsCard(),
  378. // 选中位置(前景层)
  379. _buildSelectedLocationCard(),
  380. ],
  381. );
  382. });
  383. }
  384. /// 构建选中位置卡片
  385. Widget _buildSelectedLocationCard() {
  386. return GestureDetector(
  387. onTap: () {
  388. controller.collapseRecentLocations();
  389. Get.toNamed(Routes.NODE);
  390. },
  391. child: Obx(() {
  392. final isLight = ReactiveTheme.isLightTheme;
  393. return Container(
  394. height: 56.w,
  395. width: double.maxFinite,
  396. padding: EdgeInsets.only(left: 16.w, right: 10.w),
  397. decoration: BoxDecoration(
  398. color: Get.reactiveTheme.highlightColor,
  399. borderRadius: BorderRadius.circular(12.r),
  400. boxShadow: isLight
  401. ? [
  402. BoxShadow(
  403. color: Colors.black.withOpacity(0.06),
  404. offset: const Offset(0, 4),
  405. blurRadius: 16,
  406. spreadRadius: 0,
  407. ),
  408. ]
  409. : null,
  410. ),
  411. child: Row(
  412. children: [
  413. // 国旗图标
  414. CountryIcon(
  415. countryCode: controller.selectedLocation?.country ?? '',
  416. width: 32.w,
  417. height: 24.w,
  418. borderRadius: 4.r,
  419. ),
  420. 10.horizontalSpace,
  421. // 位置名称
  422. Expanded(
  423. child: Text(
  424. controller.selectedLocation?.name ?? '',
  425. style: isDesktop
  426. ? TextStyle(
  427. fontSize: 14.sp,
  428. fontWeight: FontWeight.w500,
  429. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  430. )
  431. : TextStyle(
  432. fontSize: 16.sp,
  433. height: 1.5,
  434. fontWeight: FontWeight.w500,
  435. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  436. ),
  437. ),
  438. ),
  439. // 箭头图标
  440. Icon(
  441. IconFont.icon02,
  442. size: 20.w,
  443. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  444. ),
  445. ],
  446. ),
  447. ).withClickCursor(isDesktop);
  448. }),
  449. );
  450. }
  451. /// 构建最近位置卡片(支持展开/收缩)
  452. Widget _buildRecentLocationsCard() {
  453. return Obx(() {
  454. final isLight = ReactiveTheme.isLightTheme;
  455. return Container(
  456. margin: EdgeInsets.symmetric(horizontal: 10.w),
  457. padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 56.w, bottom: 0),
  458. decoration: BoxDecoration(
  459. color: Get.reactiveTheme.cardColor,
  460. borderRadius: BorderRadius.circular(12.r),
  461. border: isLight
  462. ? Border.all(color: Get.reactiveTheme.dividerColor, width: 1.w)
  463. : null,
  464. ),
  465. child: Column(
  466. children: [
  467. GestureDetector(
  468. behavior: HitTestBehavior.opaque,
  469. onTap: () {
  470. controller.isRecentLocationsExpanded =
  471. !controller.isRecentLocationsExpanded;
  472. },
  473. child: SizedBox(
  474. height: 44.w,
  475. child: Row(
  476. children: [
  477. Icon(
  478. IconFont.icon68,
  479. size: 16.w,
  480. color: Get.reactiveTheme.hintColor,
  481. ),
  482. SizedBox(width: 4.w),
  483. Text(
  484. Strings.recent.tr,
  485. style: TextStyle(
  486. fontSize: 12.sp,
  487. height: 1.2,
  488. color: Get.reactiveTheme.hintColor,
  489. ),
  490. ),
  491. const Spacer(),
  492. // 最近三个节点的国旗图标(收缩状态)或箭头(展开状态)
  493. Obx(() {
  494. return AnimatedOpacity(
  495. opacity: controller.isRecentLocationsExpanded
  496. ? 0.0
  497. : 1.0,
  498. duration: const Duration(milliseconds: 300),
  499. child: IgnorePointer(
  500. ignoring: controller.isRecentLocationsExpanded,
  501. child: Row(
  502. mainAxisSize: MainAxisSize.min,
  503. children: [
  504. ...controller.recentLocations.take(3).map((
  505. location,
  506. ) {
  507. return Container(
  508. margin: EdgeInsets.only(right: 4.w),
  509. decoration: BoxDecoration(
  510. borderRadius: BorderRadius.circular(5.r),
  511. border: Border.all(
  512. color: Get.reactiveTheme.canvasColor,
  513. width: 0.4.w,
  514. ),
  515. ),
  516. child: CountryIcon(
  517. countryCode: location.country ?? '',
  518. width: 24.w,
  519. height: 16.w,
  520. borderRadius: 4.r,
  521. ),
  522. );
  523. }),
  524. ],
  525. ),
  526. ),
  527. );
  528. }),
  529. Obx(() {
  530. return AnimatedRotation(
  531. turns: controller.isRecentLocationsExpanded
  532. ? 0.25
  533. : 0.0,
  534. duration: const Duration(milliseconds: 300),
  535. child: Icon(
  536. IconFont.icon02,
  537. size: 20.w,
  538. color: Get.reactiveTheme.hintColor,
  539. ),
  540. );
  541. }),
  542. ],
  543. ),
  544. ),
  545. ),
  546. // 最近位置列表(可折叠)
  547. Obx(() {
  548. return ClipRect(
  549. child: AnimatedAlign(
  550. duration: const Duration(milliseconds: 300),
  551. curve: Curves.easeInOut,
  552. heightFactor: controller.isRecentLocationsExpanded
  553. ? 1.0
  554. : 0.0,
  555. alignment: Alignment.topLeft,
  556. child: AnimatedOpacity(
  557. opacity: controller.isRecentLocationsExpanded ? 1.0 : 0.0,
  558. duration: const Duration(milliseconds: 300),
  559. child: Column(
  560. children: controller.recentLocations.map((location) {
  561. return ClickOpacity(
  562. onTap: () {
  563. controller.isRecentLocationsExpanded =
  564. !controller.isRecentLocationsExpanded;
  565. controller.selectLocation(location);
  566. controller.handleConnect();
  567. },
  568. child: Column(
  569. children: [
  570. Divider(
  571. height: 1,
  572. color: Get.reactiveTheme.dividerColor,
  573. ),
  574. Container(
  575. margin: EdgeInsets.symmetric(vertical: 12.h),
  576. child: Row(
  577. mainAxisAlignment: MainAxisAlignment.center,
  578. crossAxisAlignment: CrossAxisAlignment.center,
  579. children: [
  580. SizedBox(width: 2.w),
  581. // 国旗图标
  582. CountryIcon(
  583. countryCode: location.country ?? '',
  584. width: 28.w,
  585. height: 21.w,
  586. borderRadius: 4.r,
  587. ),
  588. SizedBox(width: 10.w),
  589. // 位置信息
  590. Expanded(
  591. child: Text(
  592. location.name ?? '',
  593. style: TextStyle(
  594. fontSize: 14.sp,
  595. fontWeight: FontWeight.w500,
  596. color: Get.reactiveTheme.hintColor,
  597. ),
  598. ),
  599. ),
  600. ],
  601. ),
  602. ),
  603. ],
  604. ),
  605. );
  606. }).toList(),
  607. ),
  608. ),
  609. ),
  610. );
  611. }),
  612. ],
  613. ),
  614. ).withClickCursor(isDesktop);
  615. });
  616. }
  617. }