home_view.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. Obx(
  36. () => ClickOpacity(
  37. onTap: () => Get.toNamed(Routes.SUBSCRIPTION),
  38. child: IXImage(
  39. source: controller.apiController.userLevel == 3
  40. ? Assets.premium
  41. : controller.apiController.userLevel == 9999
  42. ? Assets.test
  43. : Assets.free,
  44. width: controller.apiController.userLevel == 3
  45. ? 92.w
  46. : 64.w,
  47. height: 28.w,
  48. sourceType: ImageSourceType.asset,
  49. ),
  50. ),
  51. ),
  52. Obx(
  53. () => Text(
  54. controller.apiController.isGuest &&
  55. !controller.apiController.isPremium &&
  56. controller.apiController.remainTimeSeconds > 0
  57. ? controller.apiController.remainTimeFormatted
  58. : controller.coreController.timer,
  59. style: TextStyle(
  60. fontSize: 28.sp,
  61. height: 1.2,
  62. color: Get.reactiveTheme.primaryColor,
  63. ),
  64. ),
  65. ),
  66. ClickOpacity(
  67. child: Padding(
  68. padding: EdgeInsets.only(
  69. left: 10.w,
  70. right: 0.w,
  71. top: 10.w,
  72. bottom: 10.w,
  73. ),
  74. child: Icon(
  75. IconFont.icon09,
  76. size: 26.w,
  77. color: Get.reactiveTheme.hintColor,
  78. ),
  79. ),
  80. onTap: () {
  81. Get.toNamed(Routes.SETTING);
  82. // ErrorDialog.show(
  83. // message:
  84. // "The VPN was disconnected unexpectedly. Would you like to reconnect now to stay protected?",
  85. // );
  86. },
  87. ),
  88. ],
  89. ),
  90. 20.verticalSpaceFromWidth,
  91. Expanded(
  92. child: SmartRefresher(
  93. enablePullDown: true,
  94. enablePullUp: false,
  95. controller: controller.refreshController,
  96. onRefresh: controller.onRefresh,
  97. child: SingleChildScrollView(
  98. physics: const ClampingScrollPhysics(),
  99. child: Column(
  100. crossAxisAlignment: CrossAxisAlignment.start,
  101. children: [
  102. // 位置选择按钮和最近位置(叠在一起的效果)
  103. 20.verticalSpaceFromWidth,
  104. Stack(
  105. children: [
  106. Container(
  107. alignment: Alignment.center,
  108. margin: EdgeInsets.only(top: 138.w),
  109. child: _buildConnectionButton(),
  110. ),
  111. _buildLocationStack(),
  112. ],
  113. ),
  114. ],
  115. ),
  116. ),
  117. ),
  118. ),
  119. ],
  120. ),
  121. Positioned(
  122. bottom: Platform.isAndroid ? 10.w : 0,
  123. left: 0,
  124. right: 0,
  125. child: Column(
  126. children: [
  127. Padding(
  128. padding: EdgeInsets.symmetric(vertical: 14.w),
  129. child: CarouselSlider(
  130. options: CarouselOptions(
  131. height: 80.w,
  132. viewportFraction: 1.0,
  133. ),
  134. items: [1, 2, 3, 4, 5].map((i) {
  135. return Builder(
  136. builder: (BuildContext context) {
  137. return IXImage(
  138. source: Assets.bannerTest,
  139. width: double.infinity,
  140. height: 80.w,
  141. sourceType: ImageSourceType.asset,
  142. borderRadius: 14.r,
  143. );
  144. },
  145. );
  146. }).toList(),
  147. ),
  148. ),
  149. MenuList(),
  150. ],
  151. ),
  152. ),
  153. ],
  154. ),
  155. ),
  156. );
  157. }
  158. Widget _buildConnectionButton() {
  159. return Obx(
  160. () => ConnectionRoundButton(
  161. state: controller.coreController.state,
  162. onTap: () {
  163. controller.setDefaultAutoConnect();
  164. },
  165. ),
  166. );
  167. }
  168. /// 构建位置堆叠效果(选中位置 + 最近位置)
  169. Widget _buildLocationStack() {
  170. return Obx(() {
  171. if (controller.selectedLocation == null) {
  172. return const SizedBox.shrink();
  173. }
  174. return Stack(
  175. children: [
  176. // 最近位置列表(背景层)
  177. if (controller.recentLocations.isNotEmpty)
  178. _buildRecentLocationsCard(),
  179. // 选中位置(前景层)
  180. _buildSelectedLocationCard(),
  181. ],
  182. );
  183. });
  184. }
  185. /// 构建选中位置卡片
  186. Widget _buildSelectedLocationCard() {
  187. return GestureDetector(
  188. onTap: () {
  189. Get.toNamed(Routes.NODE);
  190. },
  191. child: Obx(() {
  192. return Container(
  193. height: 56.w,
  194. width: double.maxFinite,
  195. padding: EdgeInsets.only(left: 16.w, right: 10.w),
  196. decoration: BoxDecoration(
  197. color: Get.reactiveTheme.highlightColor,
  198. borderRadius: BorderRadius.circular(12.r),
  199. ),
  200. child: Row(
  201. children: [
  202. // 国旗图标
  203. CountryIcon(
  204. countryCode: controller.selectedLocation?.country ?? '',
  205. width: 32.w,
  206. height: 24.w,
  207. borderRadius: 4.r,
  208. ),
  209. 10.horizontalSpace,
  210. // 位置名称
  211. Expanded(
  212. child: Text(
  213. '${controller.selectedLocation?.code ?? ''} - ${controller.selectedLocation?.name ?? ''}',
  214. style: TextStyle(
  215. fontSize: 16.sp,
  216. height: 1.5,
  217. fontWeight: FontWeight.w500,
  218. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  219. ),
  220. ),
  221. ),
  222. // 箭头图标
  223. Icon(
  224. IconFont.icon02,
  225. size: 20.w,
  226. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  227. ),
  228. ],
  229. ),
  230. );
  231. }),
  232. );
  233. }
  234. /// 构建最近位置卡片(支持展开/收缩)
  235. Widget _buildRecentLocationsCard() {
  236. return Obx(() {
  237. return Container(
  238. margin: EdgeInsets.symmetric(horizontal: 10.w),
  239. padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 56.w, bottom: 0),
  240. decoration: BoxDecoration(
  241. color: Get.reactiveTheme.cardColor,
  242. borderRadius: BorderRadius.circular(12.r),
  243. ),
  244. child: Column(
  245. children: [
  246. GestureDetector(
  247. behavior: HitTestBehavior.opaque,
  248. onTap: () {
  249. controller.isRecentLocationsExpanded =
  250. !controller.isRecentLocationsExpanded;
  251. },
  252. child: SizedBox(
  253. height: 44.w,
  254. child: Row(
  255. children: [
  256. Icon(
  257. IconFont.icon68,
  258. size: 16.w,
  259. color: Get.reactiveTheme.hintColor,
  260. ),
  261. SizedBox(width: 4.w),
  262. Text(
  263. Strings.recent.tr,
  264. style: TextStyle(
  265. fontSize: 12.sp,
  266. height: 1.2,
  267. color: Get.reactiveTheme.hintColor,
  268. ),
  269. ),
  270. const Spacer(),
  271. // 最近三个节点的国旗图标(收缩状态)或箭头(展开状态)
  272. Obx(() {
  273. return AnimatedOpacity(
  274. opacity: controller.isRecentLocationsExpanded
  275. ? 0.0
  276. : 1.0,
  277. duration: const Duration(milliseconds: 300),
  278. child: IgnorePointer(
  279. ignoring: controller.isRecentLocationsExpanded,
  280. child: Row(
  281. mainAxisSize: MainAxisSize.min,
  282. children: [
  283. ...controller.recentLocations.take(3).map((
  284. location,
  285. ) {
  286. return Container(
  287. margin: EdgeInsets.only(right: 4.w),
  288. decoration: BoxDecoration(
  289. borderRadius: BorderRadius.circular(5.r),
  290. border: Border.all(
  291. color: Get.reactiveTheme.canvasColor,
  292. width: 0.4.w,
  293. ),
  294. ),
  295. child: CountryIcon(
  296. countryCode: location.country ?? '',
  297. width: 24.w,
  298. height: 16.w,
  299. borderRadius: 4.r,
  300. ),
  301. );
  302. }),
  303. ],
  304. ),
  305. ),
  306. );
  307. }),
  308. Obx(() {
  309. return AnimatedRotation(
  310. turns: controller.isRecentLocationsExpanded
  311. ? 0.25
  312. : 0.0,
  313. duration: const Duration(milliseconds: 300),
  314. child: Icon(
  315. IconFont.icon02,
  316. size: 20.w,
  317. color: Get.reactiveTheme.hintColor,
  318. ),
  319. );
  320. }),
  321. ],
  322. ),
  323. ),
  324. ),
  325. // 最近位置列表(可折叠)
  326. Obx(() {
  327. return ClipRect(
  328. child: AnimatedAlign(
  329. duration: const Duration(milliseconds: 300),
  330. curve: Curves.easeInOut,
  331. heightFactor: controller.isRecentLocationsExpanded
  332. ? 1.0
  333. : 0.0,
  334. alignment: Alignment.topLeft,
  335. child: AnimatedOpacity(
  336. opacity: controller.isRecentLocationsExpanded ? 1.0 : 0.0,
  337. duration: const Duration(milliseconds: 300),
  338. child: Column(
  339. children: controller.recentLocations.map((location) {
  340. return ClickOpacity(
  341. onTap: () {
  342. controller.isRecentLocationsExpanded =
  343. !controller.isRecentLocationsExpanded;
  344. controller.selectLocation(location);
  345. controller.handleConnect();
  346. },
  347. child: Column(
  348. children: [
  349. Divider(
  350. height: 1,
  351. color: Get.reactiveTheme.dividerColor,
  352. ),
  353. Container(
  354. margin: EdgeInsets.symmetric(vertical: 12.h),
  355. child: Row(
  356. mainAxisAlignment: MainAxisAlignment.center,
  357. crossAxisAlignment: CrossAxisAlignment.center,
  358. children: [
  359. // 国旗图标
  360. CountryIcon(
  361. countryCode: location.country ?? '',
  362. width: 28.w,
  363. height: 21.w,
  364. borderRadius: 4.r,
  365. ),
  366. SizedBox(width: 10.w),
  367. // 位置信息
  368. Expanded(
  369. child: Text(
  370. '${location.code} - ${location.name}',
  371. style: TextStyle(
  372. fontSize: 14.sp,
  373. fontWeight: FontWeight.w500,
  374. color: Get.reactiveTheme.hintColor,
  375. ),
  376. ),
  377. ),
  378. ],
  379. ),
  380. ),
  381. ],
  382. ),
  383. );
  384. }).toList(),
  385. ),
  386. ),
  387. ),
  388. );
  389. }),
  390. ],
  391. ),
  392. );
  393. });
  394. }
  395. }