home_view.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 '../widgets/connection_round_button.dart';
  19. import '../widgets/menu_list.dart';
  20. class HomeView extends BaseView<HomeController> {
  21. const HomeView({super.key});
  22. @override
  23. bool get isPopScope => true;
  24. @override
  25. Widget buildContent(BuildContext context) {
  26. return _buildCustomScrollView();
  27. }
  28. Widget _buildCustomScrollView() {
  29. return SafeArea(
  30. child: Padding(
  31. padding: EdgeInsets.symmetric(horizontal: 14.w),
  32. child: Column(
  33. children: [
  34. _buildAppBar(),
  35. 20.verticalSpaceFromWidth,
  36. Expanded(
  37. child: LayoutBuilder(
  38. builder: (context, constraints) {
  39. // 确保 viewportHeight 不会为负数(键盘弹出时)
  40. final double viewportHeight = (constraints.maxHeight - 388.w)
  41. .clamp(0.0, double.infinity);
  42. return GestureDetector(
  43. behavior: HitTestBehavior.translucent,
  44. onTap: () {
  45. controller.collapseRecentLocations();
  46. },
  47. child: SmartRefresher(
  48. enablePullDown: true,
  49. enablePullUp: false,
  50. controller: controller.refreshController,
  51. onRefresh: controller.onRefresh,
  52. child: CustomScrollView(
  53. slivers: [
  54. // 1. 顶部和中间部分
  55. SliverList(
  56. delegate: SliverChildListDelegate([
  57. 20.verticalSpaceFromWidth,
  58. Stack(
  59. children: [
  60. Container(
  61. alignment: Alignment.center,
  62. margin: EdgeInsets.only(top: 138.w),
  63. child: _buildConnectionButton(),
  64. ),
  65. _buildLocationStack(),
  66. ],
  67. ),
  68. 20.verticalSpaceFromWidth,
  69. // // 第二部分:中间(内容可长可短)
  70. // Container(
  71. // color: Colors.green[100],
  72. // height: 400, // 修改这个高度来测试滚动效果
  73. // child: Center(child: Text("中间内容区域")),
  74. // ),
  75. ]),
  76. ),
  77. // 2. 底部部分:关键所在
  78. SliverFillRemaining(
  79. hasScrollBody: false,
  80. child: ConstrainedBox(
  81. constraints: BoxConstraints(
  82. minHeight: viewportHeight,
  83. ),
  84. child: Column(
  85. children: [
  86. Spacer(), // 自动撑开上方空间,将底部内容挤下去
  87. Padding(
  88. padding: EdgeInsets.symmetric(
  89. vertical: 14.w,
  90. ),
  91. child: CarouselSlider(
  92. options: CarouselOptions(
  93. height: 80.w,
  94. viewportFraction: 1.0,
  95. ),
  96. items: [1, 2, 3, 4, 5].map((i) {
  97. return Builder(
  98. builder: (BuildContext context) {
  99. return IXImage(
  100. source: Assets.bannerTest,
  101. width: double.infinity,
  102. height: 80.w,
  103. sourceType: ImageSourceType.asset,
  104. borderRadius: 14.r,
  105. );
  106. },
  107. );
  108. }).toList(),
  109. ),
  110. ),
  111. MenuList(),
  112. ],
  113. ),
  114. ),
  115. ),
  116. ],
  117. ),
  118. ),
  119. );
  120. },
  121. ),
  122. ),
  123. ],
  124. ),
  125. ),
  126. );
  127. }
  128. Widget _buildAppBar() {
  129. return Row(
  130. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  131. children: [
  132. Obx(() {
  133. final bgColor = controller.apiController.userLevel == 3
  134. ? DarkThemeColors.homePremiumColor
  135. : controller.apiController.userLevel == 9999
  136. ? DarkThemeColors.homeTestColor
  137. : DarkThemeColors.homeFreeColor;
  138. return ClipRRect(
  139. borderRadius: BorderRadius.circular(100.r),
  140. child: Container(
  141. decoration: BoxDecoration(color: bgColor.withValues(alpha: 0.05)),
  142. child: Stack(
  143. children: [
  144. // 左上角光晕
  145. Positioned(
  146. left: -8.w,
  147. top: -16.w,
  148. child: Container(
  149. width: 32.w,
  150. height: 32.w,
  151. decoration: BoxDecoration(
  152. shape: BoxShape.circle,
  153. gradient: RadialGradient(
  154. colors: [
  155. bgColor.withValues(alpha: 0.85),
  156. bgColor.withValues(alpha: 0.05),
  157. ],
  158. stops: const [0.0, 1.0],
  159. ),
  160. ),
  161. ),
  162. ),
  163. // 右下角光晕
  164. Positioned(
  165. right: 12.w,
  166. bottom: -12.w,
  167. child: ImageFiltered(
  168. imageFilter: ImageFilter.blur(
  169. sigmaX: 8,
  170. sigmaY: 8,
  171. tileMode: TileMode.decal,
  172. ),
  173. child: Container(
  174. width: 42.w,
  175. height: 16.w,
  176. decoration: BoxDecoration(
  177. borderRadius: BorderRadius.circular(8.w),
  178. color: bgColor.withValues(alpha: 0.85),
  179. ),
  180. ),
  181. ),
  182. ),
  183. // 内容
  184. Padding(
  185. padding: EdgeInsets.symmetric(
  186. horizontal: 6.w,
  187. vertical: 4.w,
  188. ),
  189. child: Row(
  190. children: [
  191. Obx(
  192. () => ClickOpacity(
  193. onTap: () => Get.toNamed(Routes.SUBSCRIPTION),
  194. child: IXImage(
  195. source: controller.apiController.userLevel == 3
  196. ? Assets.homePremium
  197. : controller.apiController.userLevel == 9999
  198. ? Assets.homeTest
  199. : Assets.homeFree,
  200. width: controller.apiController.userLevel == 3
  201. ? 92.w
  202. : 64.w,
  203. height: 28.w,
  204. sourceType: ImageSourceType.asset,
  205. ),
  206. ),
  207. ),
  208. Obx(
  209. () => Text(
  210. controller.apiController.isGuest &&
  211. !controller.apiController.isPremium &&
  212. controller.apiController.remainTimeSeconds >
  213. 0
  214. ? controller.apiController.remainTimeFormatted
  215. : controller.coreController.timer,
  216. style: TextStyle(
  217. fontSize: 13.sp,
  218. height: 1.5,
  219. fontStyle: FontStyle.italic,
  220. fontWeight: FontWeight.w500,
  221. fontFeatures: [FontFeature.tabularFigures()],
  222. color:
  223. controller.apiController.userLevel == 3 ||
  224. controller.apiController.userLevel == 9999
  225. ? Get.reactiveTheme.textTheme.bodyLarge!.color
  226. : Get.reactiveTheme.hintColor,
  227. ),
  228. ),
  229. ),
  230. ],
  231. ),
  232. ),
  233. ],
  234. ),
  235. ),
  236. );
  237. }),
  238. ClickOpacity(
  239. child: Padding(
  240. padding: EdgeInsets.only(
  241. left: 10.w,
  242. right: 0.w,
  243. top: 10.w,
  244. bottom: 10.w,
  245. ),
  246. child: Icon(
  247. IconFont.icon09,
  248. size: 26.w,
  249. color: Get.reactiveTheme.hintColor,
  250. ),
  251. ),
  252. onTap: () {
  253. controller.collapseRecentLocations();
  254. Get.toNamed(Routes.SETTING);
  255. },
  256. ),
  257. ],
  258. );
  259. }
  260. Widget _buildConnectionButton() {
  261. return Obx(
  262. () => ConnectionRoundButton(
  263. state: controller.coreController.state,
  264. onTap: () {
  265. controller.collapseRecentLocations();
  266. controller.setDefaultAutoConnect();
  267. },
  268. ),
  269. );
  270. }
  271. /// 构建位置堆叠效果(选中位置 + 最近位置)
  272. Widget _buildLocationStack() {
  273. return Obx(() {
  274. if (controller.selectedLocation == null) {
  275. return const SizedBox.shrink();
  276. }
  277. return Stack(
  278. children: [
  279. // 最近位置列表(背景层)
  280. if (controller.recentLocations.isNotEmpty)
  281. _buildRecentLocationsCard(),
  282. // 选中位置(前景层)
  283. _buildSelectedLocationCard(),
  284. ],
  285. );
  286. });
  287. }
  288. /// 构建选中位置卡片
  289. Widget _buildSelectedLocationCard() {
  290. return GestureDetector(
  291. onTap: () {
  292. controller.collapseRecentLocations();
  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. }