node_list.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 nodeController = Get.find<NodeController>();
  29. final apiController = Get.find<ApiController>();
  30. /// 获取国家的展开状态
  31. bool _getExpandedState(String countryCode) {
  32. return nodeController.getExpandedState(widget.tabIndex, countryCode);
  33. }
  34. /// 设置国家的展开状态
  35. void _setExpandedState(String countryCode, bool expanded) {
  36. setState(() {
  37. nodeController.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. nodeController.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 = nodeController.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 nodeController = Get.find<NodeController>();
  144. final hasSelectedLocation = locations.any(
  145. (loc) => loc.id == homeController.selectedLocation?.id,
  146. );
  147. // 根据展开状态和是否选中设置背景色
  148. final backgroundColor = hasSelectedLocation
  149. ? (expanded
  150. ? Get.reactiveTheme.cardColor
  151. : Get.reactiveTheme.highlightColor)
  152. : Get.reactiveTheme.highlightColor;
  153. return AnimatedContainer(
  154. duration: const Duration(milliseconds: 300),
  155. curve: Curves.easeInOut,
  156. margin: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.w),
  157. decoration: BoxDecoration(
  158. color: backgroundColor,
  159. borderRadius: BorderRadius.circular(12.r),
  160. ),
  161. child: Column(
  162. children: [
  163. ClickOpacity(
  164. onTap: () => onExpandedChanged(!expanded),
  165. child: Padding(
  166. padding: EdgeInsets.all(14.w),
  167. child: Row(
  168. children: [
  169. // 国旗图标
  170. CountryIcon(
  171. countryCode: countryIcon,
  172. width: 32.w,
  173. height: 24.w,
  174. borderRadius: 4.r,
  175. ),
  176. 10.horizontalSpace,
  177. Text(
  178. countryName,
  179. style: TextStyle(
  180. fontSize: 16.sp,
  181. height: 1.5,
  182. fontWeight: FontWeight.w500,
  183. color: hasSelectedLocation
  184. ? Get.reactiveTheme.primaryColor
  185. : Get.reactiveTheme.textTheme.bodyLarge!.color,
  186. ),
  187. ),
  188. const Spacer(),
  189. // 箭头图标
  190. AnimatedRotation(
  191. turns: expanded ? -0.25 : 0.25,
  192. duration: const Duration(milliseconds: 300),
  193. child: Icon(
  194. IconFont.icon02,
  195. size: 20.w,
  196. color: expanded
  197. ? Get.reactiveTheme.textTheme.bodyLarge!.color
  198. : Get.reactiveTheme.hintColor,
  199. ),
  200. ),
  201. ],
  202. ),
  203. ),
  204. ),
  205. ClipRect(
  206. child: AnimatedAlign(
  207. duration: const Duration(milliseconds: 300),
  208. curve: Curves.easeInOut,
  209. heightFactor: expanded ? 1.0 : 0.0,
  210. alignment: Alignment.topLeft,
  211. child: AnimatedOpacity(
  212. opacity: expanded ? 1.0 : 0.0,
  213. duration: const Duration(milliseconds: 300),
  214. child: Column(
  215. children: locations.map((location) {
  216. final locationName = location.name ?? '';
  217. final locationIcon = location.icon ?? countryIcon;
  218. // 判断当前节点是否被选中
  219. final isSelected =
  220. location.id == homeController.selectedLocation?.id;
  221. return ClickOpacity(
  222. onTap: () {
  223. homeController.selectLocation(
  224. location,
  225. locationSelectionType: nodeController.getCurrentTab(),
  226. );
  227. homeController.handleConnect(delay: true);
  228. // 返回上一页
  229. Get.back();
  230. },
  231. child: Column(
  232. children: [
  233. Divider(
  234. height: 1,
  235. color: Get.reactiveTheme.dividerColor,
  236. ),
  237. Container(
  238. alignment: Alignment.centerLeft,
  239. margin: EdgeInsets.only(
  240. top: 14.w,
  241. bottom: 14.w,
  242. left: 22.w,
  243. right: 14.w,
  244. ),
  245. child: Row(
  246. children: [
  247. CountryIcon(
  248. countryCode: locationIcon,
  249. width: 24.w,
  250. height: 18.w,
  251. borderRadius: 4.r,
  252. ),
  253. 10.horizontalSpace,
  254. Expanded(
  255. child: Text(
  256. locationName,
  257. style: TextStyle(
  258. fontSize: 14.sp,
  259. fontWeight: FontWeight.w400,
  260. color: isSelected
  261. ? Get.reactiveTheme.primaryColor
  262. : Get
  263. .reactiveTheme
  264. .textTheme
  265. .bodyLarge!
  266. .color,
  267. ),
  268. ),
  269. ),
  270. // 显示延迟
  271. if (isSelected)
  272. Icon(
  273. IconFont.icon27,
  274. size: 16.w,
  275. color: Get.reactiveTheme.primaryColor,
  276. ),
  277. ],
  278. ),
  279. ),
  280. ],
  281. ),
  282. );
  283. }).toList(),
  284. ),
  285. ),
  286. ),
  287. ),
  288. ],
  289. ),
  290. );
  291. }
  292. }