node_list.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:flutter_sticky_header/flutter_sticky_header.dart';
  4. import 'package:flutter_svg/flutter_svg.dart';
  5. import 'package:get/get.dart';
  6. import 'package:nomo/app/widgets/click_opacity.dart';
  7. import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';
  8. import '../../../constants/assets.dart';
  9. import '../../../data/models/launch/groups.dart';
  10. import '../../home/controllers/home_controller.dart';
  11. import '../controllers/node_controller.dart';
  12. class NodeList extends StatefulWidget {
  13. final int tabIndex;
  14. const NodeList({super.key, required this.tabIndex});
  15. @override
  16. State<NodeList> createState() {
  17. return _NodeListState();
  18. }
  19. }
  20. class _NodeListState extends State<NodeList>
  21. with AutomaticKeepAliveClientMixin {
  22. @override
  23. bool get wantKeepAlive => true;
  24. // 获取 Controller
  25. NodeController get controller => Get.find<NodeController>();
  26. /// 获取国家的展开状态
  27. bool _getExpandedState(String countryCode) {
  28. return controller.getExpandedState(widget.tabIndex, countryCode);
  29. }
  30. /// 设置国家的展开状态
  31. void _setExpandedState(String countryCode, bool expanded) {
  32. setState(() {
  33. controller.setExpandedState(widget.tabIndex, countryCode, expanded);
  34. });
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. super.build(context);
  39. // 获取当前 tab 的数据
  40. final data = controller.getDataByTabIndex(widget.tabIndex);
  41. if (data == null || data.tags == null || data.list == null) {
  42. return Center(
  43. child: Text(
  44. '暂无数据',
  45. style: TextStyle(fontSize: 16.sp, color: Get.reactiveTheme.hintColor),
  46. ),
  47. );
  48. }
  49. // 按 tag 分组数据
  50. final groupedData = <int, List<dynamic>>{};
  51. for (var location in data.list!) {
  52. if (location.tag != null) {
  53. groupedData.putIfAbsent(location.tag!, () => []).add(location);
  54. }
  55. }
  56. // 创建 tag 名称映射
  57. final tagNameMap = <int, String>{};
  58. for (var tag in data.tags!) {
  59. if (tag.id != null && tag.name != null) {
  60. tagNameMap[tag.id!] = tag.name!;
  61. }
  62. }
  63. // 按 sort 排序 tags
  64. final sortedTags = data.tags!.toList()
  65. ..sort((a, b) => (a.sort ?? 0).compareTo(b.sort ?? 0));
  66. return CustomScrollView(
  67. slivers: [
  68. for (var tag in sortedTags)
  69. if (groupedData.containsKey(tag.id) &&
  70. groupedData[tag.id]!.isNotEmpty)
  71. SliverStickyHeader(
  72. header: Container(
  73. color: Get.reactiveTheme.scaffoldBackgroundColor,
  74. padding: const EdgeInsets.all(16),
  75. child: Text(
  76. tag.name ?? '',
  77. style: TextStyle(
  78. color: Get.reactiveTheme.hintColor,
  79. fontSize: 16,
  80. fontWeight: FontWeight.bold,
  81. ),
  82. ),
  83. ),
  84. sliver: SliverList(
  85. delegate: SliverChildBuilderDelegate((context, i) {
  86. final locationList = groupedData[tag.id]![i];
  87. final countryCode = locationList.icon ?? '';
  88. return _CountrySection(
  89. locationList: locationList,
  90. // 传递展开状态
  91. expanded: _getExpandedState(countryCode),
  92. // 展开状态变化回调
  93. onExpandedChanged: (expanded) {
  94. _setExpandedState(countryCode, expanded);
  95. },
  96. );
  97. }, childCount: groupedData[tag.id]!.length),
  98. ),
  99. ),
  100. ],
  101. );
  102. }
  103. }
  104. class _CountrySection extends StatelessWidget {
  105. final LocationList locationList;
  106. final bool expanded;
  107. final ValueChanged<bool> onExpandedChanged;
  108. const _CountrySection({
  109. required this.locationList,
  110. required this.expanded,
  111. required this.onExpandedChanged,
  112. });
  113. @override
  114. Widget build(BuildContext context) {
  115. final countryIcon = locationList.icon ?? '';
  116. final countryName = locationList.name ?? '';
  117. final locations = locationList.locations ?? [];
  118. return Container(
  119. margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
  120. decoration: BoxDecoration(
  121. color: Get.reactiveTheme.highlightColor,
  122. borderRadius: BorderRadius.circular(12),
  123. ),
  124. child: Column(
  125. children: [
  126. ClickOpacity(
  127. onTap: () => onExpandedChanged(!expanded),
  128. child: Padding(
  129. padding: EdgeInsets.all(14.w),
  130. child: Row(
  131. children: [
  132. // 国旗图标
  133. ClipRRect(
  134. borderRadius: BorderRadius.circular(4.r),
  135. child: SvgPicture.asset(
  136. Assets.getCountryFlagImage(countryIcon),
  137. width: 32.w,
  138. height: 24.w,
  139. fit: BoxFit.cover,
  140. ),
  141. ),
  142. 10.horizontalSpace,
  143. Text(
  144. countryName,
  145. style: TextStyle(
  146. fontSize: 16.sp,
  147. height: 1.5,
  148. fontWeight: FontWeight.w500,
  149. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  150. ),
  151. ),
  152. const Spacer(),
  153. // 箭头图标
  154. AnimatedRotation(
  155. turns: expanded ? 0.25 : 0.0,
  156. duration: const Duration(milliseconds: 300),
  157. child: Icon(
  158. Icons.keyboard_arrow_right,
  159. size: 20.w,
  160. color: Get.reactiveTheme.hintColor,
  161. ),
  162. ),
  163. ],
  164. ),
  165. ),
  166. ),
  167. ClipRect(
  168. child: AnimatedAlign(
  169. duration: const Duration(milliseconds: 300),
  170. curve: Curves.easeInOut,
  171. heightFactor: expanded ? 1.0 : 0.0,
  172. alignment: Alignment.topLeft,
  173. child: AnimatedOpacity(
  174. opacity: expanded ? 1.0 : 0.0,
  175. duration: const Duration(milliseconds: 300),
  176. child: Column(
  177. children: locations.map((location) {
  178. final locationName = location.name ?? '';
  179. final locationIcon = location.icon ?? countryIcon;
  180. final latency = location.latency;
  181. return ClickOpacity(
  182. onTap: () {
  183. // 获取 HomeController 并保存选中的节点
  184. final homeController = Get.find<HomeController>();
  185. homeController.selectLocation(location);
  186. homeController.handleConnect();
  187. // 返回上一页
  188. Get.back();
  189. },
  190. child: Column(
  191. children: [
  192. Divider(
  193. height: 1,
  194. color: Get.reactiveTheme.dividerColor,
  195. ),
  196. Container(
  197. alignment: Alignment.centerLeft,
  198. margin: EdgeInsets.only(
  199. top: 14.w,
  200. bottom: 14.w,
  201. left: 24.w,
  202. right: 14.w,
  203. ),
  204. child: Row(
  205. children: [
  206. ClipRRect(
  207. borderRadius: BorderRadius.circular(4.r),
  208. child: SvgPicture.asset(
  209. Assets.getCountryFlagImage(locationIcon),
  210. width: 32.w,
  211. height: 24.w,
  212. fit: BoxFit.cover,
  213. ),
  214. ),
  215. 10.horizontalSpace,
  216. Expanded(
  217. child: Text(
  218. locationName,
  219. style: TextStyle(
  220. fontSize: 14.sp,
  221. fontWeight: FontWeight.w500,
  222. color: Get.reactiveTheme.hintColor,
  223. ),
  224. ),
  225. ),
  226. // 显示延迟
  227. if (latency != null && latency > 0)
  228. Text(
  229. '${latency}ms',
  230. style: TextStyle(
  231. fontSize: 12.sp,
  232. color: Get.reactiveTheme.hintColor,
  233. ),
  234. ),
  235. ],
  236. ),
  237. ),
  238. ],
  239. ),
  240. );
  241. }).toList(),
  242. ),
  243. ),
  244. ),
  245. ),
  246. ],
  247. ),
  248. );
  249. }
  250. }