home_view.dart 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. import 'dart:ui';
  2. import 'package:carousel_slider/carousel_slider.dart';
  3. import 'package:flutter/material.dart' hide ConnectionState;
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:get/get.dart';
  6. import 'package:nomo/app/constants/iconfont/iconfont.dart';
  7. import 'package:nomo/app/data/sp/ix_sp.dart';
  8. import 'package:nomo/app/extensions/widget_extension.dart';
  9. import 'package:nomo/utils/misc.dart';
  10. import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
  11. import '../../../../config/theme/dark_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. return Stack(
  101. children: [
  102. CarouselSlider(
  103. options: CarouselOptions(
  104. height: 80.w,
  105. viewportFraction: 1.0,
  106. autoPlay:
  107. controller.bannerList.length >
  108. 1,
  109. autoPlayInterval: const Duration(
  110. seconds: 5,
  111. ),
  112. onPageChanged: (index, reason) {
  113. controller.currentBannerIndex =
  114. index;
  115. },
  116. ),
  117. items: controller.bannerList.map((
  118. banner,
  119. ) {
  120. return Builder(
  121. builder:
  122. (BuildContext context) {
  123. return GestureDetector(
  124. onTap: () => controller
  125. .onBannerTap(
  126. banner,
  127. ),
  128. child: IXImage(
  129. source:
  130. banner.img ?? '',
  131. width:
  132. double.infinity,
  133. height: 80.w,
  134. sourceType:
  135. ImageSourceType
  136. .network,
  137. borderRadius: 14.r,
  138. ),
  139. );
  140. },
  141. );
  142. }).toList(),
  143. ),
  144. if (controller.bannerList.length > 1)
  145. Positioned(
  146. bottom: 0,
  147. left: 0,
  148. right: 0,
  149. child: Row(
  150. mainAxisAlignment:
  151. MainAxisAlignment.center,
  152. children: controller.bannerList
  153. .asMap()
  154. .entries
  155. .map((entry) {
  156. return AnimatedContainer(
  157. duration:
  158. const Duration(
  159. milliseconds: 300,
  160. ),
  161. curve: Curves.easeInOut,
  162. width:
  163. controller
  164. .currentBannerIndex ==
  165. entry.key
  166. ? 16
  167. : 6,
  168. height: 6,
  169. margin:
  170. const EdgeInsets.symmetric(
  171. vertical: 8.0,
  172. horizontal: 2.0,
  173. ),
  174. decoration: BoxDecoration(
  175. borderRadius:
  176. BorderRadius.circular(
  177. 6,
  178. ),
  179. color: Colors.white
  180. .withValues(
  181. alpha:
  182. controller
  183. .currentBannerIndex ==
  184. entry
  185. .key
  186. ? 0.9
  187. : 0.4,
  188. ),
  189. ),
  190. );
  191. })
  192. .toList(),
  193. ),
  194. ),
  195. ],
  196. );
  197. }),
  198. ),
  199. MenuList(),
  200. ],
  201. ),
  202. ),
  203. ),
  204. ],
  205. ),
  206. ),
  207. );
  208. },
  209. ),
  210. ),
  211. ],
  212. ),
  213. ),
  214. );
  215. }
  216. Widget _buildAppBar() {
  217. return Row(
  218. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  219. children: [
  220. Obx(() {
  221. final bgColor = controller.apiController.userLevel == 3
  222. ? DarkThemeColors.homePremiumColor
  223. : controller.apiController.userLevel == 9999
  224. ? DarkThemeColors.homeTestColor
  225. : DarkThemeColors.homeFreeColor;
  226. return ClipRRect(
  227. borderRadius: BorderRadius.circular(100.r),
  228. child: ClickOpacity(
  229. onTap: () => Get.toNamed(Routes.SUBSCRIPTION),
  230. child: Container(
  231. decoration: BoxDecoration(
  232. color: bgColor.withValues(alpha: 0.05),
  233. ),
  234. child: Stack(
  235. children: [
  236. // 左上角光晕
  237. Positioned(
  238. left: -8.w,
  239. top: -16.w,
  240. child: Container(
  241. width: 32.w,
  242. height: 32.w,
  243. decoration: BoxDecoration(
  244. shape: BoxShape.circle,
  245. gradient: RadialGradient(
  246. colors: [
  247. bgColor.withValues(alpha: 0.85),
  248. bgColor.withValues(alpha: 0.05),
  249. ],
  250. stops: const [0.0, 1.0],
  251. ),
  252. ),
  253. ),
  254. ),
  255. // 右下角光晕
  256. Positioned(
  257. right: 12.w,
  258. bottom: -12.w,
  259. child: ImageFiltered(
  260. imageFilter: ImageFilter.blur(
  261. sigmaX: 8,
  262. sigmaY: 8,
  263. tileMode: TileMode.decal,
  264. ),
  265. child: Container(
  266. width: 42.w,
  267. height: 16.w,
  268. decoration: BoxDecoration(
  269. borderRadius: BorderRadius.circular(8.w),
  270. color: bgColor.withValues(alpha: 0.85),
  271. ),
  272. ),
  273. ),
  274. ),
  275. Padding(
  276. padding: EdgeInsets.symmetric(
  277. horizontal: 6.w,
  278. vertical: 4.w,
  279. ),
  280. child: Row(
  281. children: [
  282. Obx(
  283. () => IXImage(
  284. source: controller.apiController.userLevel == 3
  285. ? Assets.homePremium
  286. : controller.apiController.userLevel == 9999
  287. ? Assets.homeTest
  288. : Assets.homeFree,
  289. width: controller.apiController.userLevel == 3
  290. ? 92.w
  291. : 64.w,
  292. height: 28.w,
  293. sourceType: ImageSourceType.asset,
  294. ),
  295. ),
  296. _buildReminder(),
  297. ],
  298. ),
  299. ),
  300. // 倒计时提醒
  301. ],
  302. ),
  303. ),
  304. ),
  305. );
  306. }),
  307. ClickOpacity(
  308. child: Padding(
  309. padding: EdgeInsets.only(
  310. left: 10.w,
  311. right: 0.w,
  312. top: 10.w,
  313. bottom: 10.w,
  314. ),
  315. child: Icon(
  316. IconFont.icon09,
  317. size: 26.w,
  318. color: Get.reactiveTheme.hintColor,
  319. ),
  320. ),
  321. onTap: () {
  322. controller.collapseRecentLocations();
  323. Get.toNamed(Routes.SETTING);
  324. },
  325. ),
  326. ],
  327. );
  328. }
  329. Widget _buildReminder() {
  330. // return Obx(
  331. // () => Text(
  332. // controller.apiController.isGuest &&
  333. // !controller.apiController.isPremium &&
  334. // controller.apiController.remainTimeSeconds > 0
  335. // ? controller.apiController.remainTimeFormatted
  336. // : controller.coreController.timer,
  337. // style: TextStyle(
  338. // fontSize: 13.sp,
  339. // height: 1.5,
  340. // fontStyle: FontStyle.italic,
  341. // fontWeight: FontWeight.w500,
  342. // fontFeatures: [FontFeature.tabularFigures()],
  343. // color:
  344. // controller.apiController.userLevel == 3 ||
  345. // controller.apiController.userLevel == 9999
  346. // ? Get.reactiveTheme.textTheme.bodyLarge!.color
  347. // : Get.reactiveTheme.hintColor,
  348. // ),
  349. // ),
  350. // );
  351. return Obx(() {
  352. // 只监听 shouldShowCountdown 和 remainTimeFormatted
  353. // 只有文案或显示状态变化时才更新 UI
  354. if (!controller.apiController.shouldShowCountdown) {
  355. return const SizedBox.shrink();
  356. }
  357. return Text(
  358. '${controller.apiController.remainTimeFormatted} ',
  359. style: TextStyle(
  360. fontSize: 13.sp,
  361. height: 1.5,
  362. fontStyle: FontStyle.italic,
  363. fontWeight: FontWeight.w500,
  364. fontFeatures: [FontFeature.tabularFigures()],
  365. color:
  366. controller.apiController.userLevel == 3 ||
  367. controller.apiController.userLevel == 9999
  368. ? Get.reactiveTheme.textTheme.bodyLarge!.color
  369. : Get.reactiveTheme.hintColor,
  370. ),
  371. );
  372. });
  373. }
  374. Widget _buildConnectionButton() {
  375. return Obx(
  376. () => ConnectionThemeButton(
  377. state: controller.coreController.state,
  378. onTap: () {
  379. controller.collapseRecentLocations();
  380. controller.setDefaultAutoConnect();
  381. },
  382. ).withClickCursor(isDesktop),
  383. );
  384. }
  385. /// 构建位置堆叠效果(选中位置 + 最近位置)
  386. Widget _buildLocationStack() {
  387. return Obx(() {
  388. if (controller.selectedLocation == null) {
  389. return const SizedBox.shrink();
  390. }
  391. return Stack(
  392. children: [
  393. // 最近位置列表(背景层)
  394. if (controller.recentLocations.isNotEmpty)
  395. _buildRecentLocationsCard(),
  396. // 选中位置(前景层)
  397. _buildSelectedLocationCard(),
  398. ],
  399. );
  400. });
  401. }
  402. /// 构建选中位置卡片
  403. Widget _buildSelectedLocationCard() {
  404. return GestureDetector(
  405. onTap: () {
  406. controller.collapseRecentLocations();
  407. Get.toNamed(Routes.NODE);
  408. },
  409. child: Obx(() {
  410. final isLight = IXSP.getThemeIsLight();
  411. return Container(
  412. height: 56.w,
  413. width: double.maxFinite,
  414. padding: EdgeInsets.only(left: 16.w, right: 10.w),
  415. decoration: BoxDecoration(
  416. color: Get.reactiveTheme.highlightColor,
  417. borderRadius: BorderRadius.circular(12.r),
  418. boxShadow: isLight
  419. ? [
  420. BoxShadow(
  421. color: Colors.black.withOpacity(0.06),
  422. offset: const Offset(0, 4),
  423. blurRadius: 16,
  424. spreadRadius: 0,
  425. ),
  426. ]
  427. : null,
  428. ),
  429. child: Row(
  430. children: [
  431. // 国旗图标
  432. CountryIcon(
  433. countryCode: controller.selectedLocation?.country ?? '',
  434. width: 32.w,
  435. height: 24.w,
  436. borderRadius: 4.r,
  437. ),
  438. 10.horizontalSpace,
  439. // 位置名称
  440. Expanded(
  441. child: Text(
  442. '${controller.selectedLocation?.code ?? ''} - ${controller.selectedLocation?.name ?? ''}',
  443. style: TextStyle(
  444. fontSize: 16.sp,
  445. height: 1.5,
  446. fontWeight: FontWeight.w500,
  447. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  448. ),
  449. ),
  450. ),
  451. // 箭头图标
  452. Icon(
  453. IconFont.icon02,
  454. size: 20.w,
  455. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  456. ),
  457. ],
  458. ),
  459. ).withClickCursor(isDesktop);
  460. }),
  461. );
  462. }
  463. /// 构建最近位置卡片(支持展开/收缩)
  464. Widget _buildRecentLocationsCard() {
  465. return Obx(() {
  466. final isLight = IXSP.getThemeIsLight();
  467. return Container(
  468. margin: EdgeInsets.symmetric(horizontal: 10.w),
  469. padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 56.w, bottom: 0),
  470. decoration: BoxDecoration(
  471. color: Get.reactiveTheme.cardColor,
  472. borderRadius: BorderRadius.circular(12.r),
  473. border: isLight
  474. ? Border.all(color: Get.reactiveTheme.dividerColor, width: 1.w)
  475. : null,
  476. ),
  477. child: Column(
  478. children: [
  479. GestureDetector(
  480. behavior: HitTestBehavior.opaque,
  481. onTap: () {
  482. controller.isRecentLocationsExpanded =
  483. !controller.isRecentLocationsExpanded;
  484. },
  485. child: SizedBox(
  486. height: 44.w,
  487. child: Row(
  488. children: [
  489. Icon(
  490. IconFont.icon68,
  491. size: 16.w,
  492. color: Get.reactiveTheme.hintColor,
  493. ),
  494. SizedBox(width: 4.w),
  495. Text(
  496. Strings.recent.tr,
  497. style: TextStyle(
  498. fontSize: 12.sp,
  499. height: 1.2,
  500. color: Get.reactiveTheme.hintColor,
  501. ),
  502. ),
  503. const Spacer(),
  504. // 最近三个节点的国旗图标(收缩状态)或箭头(展开状态)
  505. Obx(() {
  506. return AnimatedOpacity(
  507. opacity: controller.isRecentLocationsExpanded
  508. ? 0.0
  509. : 1.0,
  510. duration: const Duration(milliseconds: 300),
  511. child: IgnorePointer(
  512. ignoring: controller.isRecentLocationsExpanded,
  513. child: Row(
  514. mainAxisSize: MainAxisSize.min,
  515. children: [
  516. ...controller.recentLocations.take(3).map((
  517. location,
  518. ) {
  519. return Container(
  520. margin: EdgeInsets.only(right: 4.w),
  521. decoration: BoxDecoration(
  522. borderRadius: BorderRadius.circular(5.r),
  523. border: Border.all(
  524. color: Get.reactiveTheme.canvasColor,
  525. width: 0.4.w,
  526. ),
  527. ),
  528. child: CountryIcon(
  529. countryCode: location.country ?? '',
  530. width: 24.w,
  531. height: 16.w,
  532. borderRadius: 4.r,
  533. ),
  534. );
  535. }),
  536. ],
  537. ),
  538. ),
  539. );
  540. }),
  541. Obx(() {
  542. return AnimatedRotation(
  543. turns: controller.isRecentLocationsExpanded
  544. ? 0.25
  545. : 0.0,
  546. duration: const Duration(milliseconds: 300),
  547. child: Icon(
  548. IconFont.icon02,
  549. size: 20.w,
  550. color: Get.reactiveTheme.hintColor,
  551. ),
  552. );
  553. }),
  554. ],
  555. ),
  556. ),
  557. ),
  558. // 最近位置列表(可折叠)
  559. Obx(() {
  560. return ClipRect(
  561. child: AnimatedAlign(
  562. duration: const Duration(milliseconds: 300),
  563. curve: Curves.easeInOut,
  564. heightFactor: controller.isRecentLocationsExpanded
  565. ? 1.0
  566. : 0.0,
  567. alignment: Alignment.topLeft,
  568. child: AnimatedOpacity(
  569. opacity: controller.isRecentLocationsExpanded ? 1.0 : 0.0,
  570. duration: const Duration(milliseconds: 300),
  571. child: Column(
  572. children: controller.recentLocations.map((location) {
  573. return ClickOpacity(
  574. onTap: () {
  575. controller.isRecentLocationsExpanded =
  576. !controller.isRecentLocationsExpanded;
  577. controller.selectLocation(location);
  578. controller.handleConnect();
  579. },
  580. child: Column(
  581. children: [
  582. Divider(
  583. height: 1,
  584. color: Get.reactiveTheme.dividerColor,
  585. ),
  586. Container(
  587. margin: EdgeInsets.symmetric(vertical: 12.h),
  588. child: Row(
  589. mainAxisAlignment: MainAxisAlignment.center,
  590. crossAxisAlignment: CrossAxisAlignment.center,
  591. children: [
  592. // 国旗图标
  593. CountryIcon(
  594. countryCode: location.country ?? '',
  595. width: 28.w,
  596. height: 21.w,
  597. borderRadius: 4.r,
  598. ),
  599. SizedBox(width: 10.w),
  600. // 位置信息
  601. Expanded(
  602. child: Text(
  603. '${location.code} - ${location.name}',
  604. style: TextStyle(
  605. fontSize: 14.sp,
  606. fontWeight: FontWeight.w500,
  607. color: Get.reactiveTheme.hintColor,
  608. ),
  609. ),
  610. ),
  611. ],
  612. ),
  613. ),
  614. ],
  615. ),
  616. );
  617. }).toList(),
  618. ),
  619. ),
  620. ),
  621. );
  622. }),
  623. ],
  624. ),
  625. ).withClickCursor(isDesktop);
  626. });
  627. }
  628. }