home_view.dart 15 KB

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