home_view.dart 21 KB

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