node_list.dart 11 KB

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