node_list.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 '../../../constants/iconfont/iconfont.dart';
  8. import '../../../data/models/launch/groups.dart';
  9. import '../../../widgets/country_icon.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. SliverSafeArea(sliver: SliverToBoxAdapter(child: 0.verticalSpace)),
  101. ],
  102. );
  103. }
  104. }
  105. class _CountrySection extends StatelessWidget {
  106. final LocationList locationList;
  107. final bool expanded;
  108. final ValueChanged<bool> onExpandedChanged;
  109. const _CountrySection({
  110. required this.locationList,
  111. required this.expanded,
  112. required this.onExpandedChanged,
  113. });
  114. @override
  115. Widget build(BuildContext context) {
  116. final countryIcon = locationList.icon ?? '';
  117. final countryName = locationList.name ?? '';
  118. final locations = locationList.locations ?? [];
  119. // 获取 HomeController 并判断当前国家是否有选中的节点
  120. final homeController = Get.find<HomeController>();
  121. final hasSelectedLocation = locations.any(
  122. (loc) => loc.id == homeController.selectedLocation?.id,
  123. );
  124. // 根据展开状态和是否选中设置背景色
  125. final backgroundColor = hasSelectedLocation
  126. ? (expanded
  127. ? Get.reactiveTheme.cardColor
  128. : Get.reactiveTheme.highlightColor)
  129. : Get.reactiveTheme.highlightColor;
  130. return AnimatedContainer(
  131. duration: const Duration(milliseconds: 300),
  132. curve: Curves.easeInOut,
  133. margin: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.w),
  134. decoration: BoxDecoration(
  135. color: backgroundColor,
  136. borderRadius: BorderRadius.circular(12.r),
  137. ),
  138. child: Column(
  139. children: [
  140. ClickOpacity(
  141. onTap: () => onExpandedChanged(!expanded),
  142. child: Padding(
  143. padding: EdgeInsets.all(14.w),
  144. child: Row(
  145. children: [
  146. // 国旗图标
  147. CountryIcon(
  148. countryCode: countryIcon,
  149. width: 32.w,
  150. height: 24.w,
  151. borderRadius: 4.r,
  152. ),
  153. 10.horizontalSpace,
  154. Text(
  155. countryName,
  156. style: TextStyle(
  157. fontSize: 16.sp,
  158. height: 1.5,
  159. fontWeight: FontWeight.w500,
  160. color: hasSelectedLocation
  161. ? Get.reactiveTheme.primaryColor
  162. : Get.reactiveTheme.textTheme.bodyLarge!.color,
  163. ),
  164. ),
  165. const Spacer(),
  166. // 箭头图标
  167. AnimatedRotation(
  168. turns: expanded ? 0.25 : 0.0,
  169. duration: const Duration(milliseconds: 300),
  170. child: Icon(
  171. IconFont.icon02,
  172. size: 20.w,
  173. color: Get.reactiveTheme.hintColor,
  174. ),
  175. ),
  176. ],
  177. ),
  178. ),
  179. ),
  180. ClipRect(
  181. child: AnimatedAlign(
  182. duration: const Duration(milliseconds: 300),
  183. curve: Curves.easeInOut,
  184. heightFactor: expanded ? 1.0 : 0.0,
  185. alignment: Alignment.topLeft,
  186. child: AnimatedOpacity(
  187. opacity: expanded ? 1.0 : 0.0,
  188. duration: const Duration(milliseconds: 300),
  189. child: Column(
  190. children: locations.map((location) {
  191. final locationName = location.name ?? '';
  192. final locationIcon = location.icon ?? countryIcon;
  193. // 判断当前节点是否被选中
  194. final isSelected =
  195. location.id == homeController.selectedLocation?.id;
  196. return ClickOpacity(
  197. onTap: () {
  198. // 获取 HomeController
  199. final homeController = Get.find<HomeController>();
  200. homeController.selectLocation(location);
  201. homeController.handleConnect(delay: true);
  202. // 返回上一页
  203. Get.back();
  204. },
  205. child: Column(
  206. children: [
  207. Divider(
  208. height: 1,
  209. color: Get.reactiveTheme.dividerColor,
  210. ),
  211. Container(
  212. alignment: Alignment.centerLeft,
  213. margin: EdgeInsets.only(
  214. top: 14.w,
  215. bottom: 14.w,
  216. left: 24.w,
  217. right: 14.w,
  218. ),
  219. child: Row(
  220. children: [
  221. CountryIcon(
  222. countryCode: locationIcon,
  223. width: 32.w,
  224. height: 24.w,
  225. borderRadius: 4.r,
  226. ),
  227. 10.horizontalSpace,
  228. Expanded(
  229. child: Text(
  230. locationName,
  231. style: TextStyle(
  232. fontSize: 14.sp,
  233. fontWeight: FontWeight.w500,
  234. color: isSelected
  235. ? Get.reactiveTheme.primaryColor
  236. : Get
  237. .reactiveTheme
  238. .textTheme
  239. .bodyLarge!
  240. .color,
  241. ),
  242. ),
  243. ),
  244. // 显示延迟
  245. if (isSelected)
  246. Icon(
  247. IconFont.icon27,
  248. size: 16.w,
  249. color: Get.reactiveTheme.primaryColor,
  250. ),
  251. ],
  252. ),
  253. ),
  254. ],
  255. ),
  256. );
  257. }).toList(),
  258. ),
  259. ),
  260. ),
  261. ),
  262. ],
  263. ),
  264. );
  265. }
  266. }