node_list.dart 12 KB

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