home_view.dart 15 KB

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