node_list.dart 12 KB

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