home_view.dart 21 KB

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