node_controller.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../../../constants/iconfont/iconfont.dart';
  4. import '../../../controllers/core_controller.dart';
  5. import '../../../data/models/launch/groups.dart';
  6. import '../../../data/sp/ix_sp.dart';
  7. class NodeController extends GetxController {
  8. final coreController = Get.find<CoreController>();
  9. // Groups 数据
  10. final _groups = Rxn<Groups>();
  11. Groups? get groups => _groups.value;
  12. set groups(Groups? value) => _groups.value = value;
  13. // 游戏tab列表
  14. final _tabTextList = <String>[].obs;
  15. List<String> get tabTextList => _tabTextList;
  16. set tabTextList(List<String> value) => _tabTextList.assignAll(value);
  17. // 当前选中的 Tab 索引
  18. final _currentTabIndex = 0.obs;
  19. int get currentTabIndex => _currentTabIndex.value;
  20. set currentTabIndex(int value) => _currentTabIndex.value = value;
  21. // 保存每个 tab 中每个国家的展开/收缩状态
  22. // key 格式: "tabIndex_countryCode"
  23. final Map<String, bool> expandedStates = {};
  24. // 当前选中的tab
  25. String getCurrentTab() {
  26. return tabTextList[currentTabIndex];
  27. }
  28. @override
  29. void onInit() {
  30. super.onInit();
  31. _loadGroups();
  32. }
  33. /// 加载 Groups 数据
  34. void _loadGroups() {
  35. final launch = IXSP.getLaunch();
  36. if (launch?.groups != null) {
  37. groups = launch!.groups;
  38. _updateTabList();
  39. }
  40. }
  41. void updateGroups(Groups? groups) {
  42. this.groups = groups;
  43. _updateTabList();
  44. }
  45. /// 更新 Tab 列表
  46. void _updateTabList() {
  47. final tabs = <String>[];
  48. // 添加 Normal tab
  49. if (groups?.normal != null && groups!.normal!.list?.isNotEmpty == true) {
  50. tabs.add('All');
  51. }
  52. // 添加 Streaming tab
  53. if (groups?.streaming != null &&
  54. groups!.streaming!.list?.isNotEmpty == true) {
  55. tabs.add('Streaming');
  56. }
  57. tabTextList = tabs;
  58. }
  59. /// 根据 tab 索引获取对应的数据
  60. Normal? getDataByTabIndex(int tabIndex) {
  61. if (tabIndex == 0) {
  62. return groups?.normal;
  63. } else if (tabIndex == 1) {
  64. return groups?.streaming;
  65. }
  66. return null;
  67. }
  68. IconData getTabIcon(int tabIndex) {
  69. if (tabIndex == 0) {
  70. return IconFont.icon24;
  71. } else if (tabIndex == 1) {
  72. return IconFont.icon25;
  73. }
  74. return IconFont.icon24;
  75. }
  76. /// 获取国家的展开状态
  77. bool getExpandedState(int tabIndex, String countryCode) {
  78. final key = '${tabIndex}_$countryCode';
  79. return expandedStates[key] ?? false; // 默认收缩
  80. }
  81. /// 设置国家的展开状态
  82. void setExpandedState(int tabIndex, String countryCode, bool expanded) {
  83. final key = '${tabIndex}_$countryCode';
  84. expandedStates[key] = expanded;
  85. }
  86. /// 当 Launch 数据更新时刷新节点
  87. void refreshOnLaunchChanged() {
  88. _loadGroups();
  89. }
  90. // 设置tab选中状态
  91. void setTabSelected(int tabIndex) {
  92. currentTabIndex = tabIndex;
  93. coreController.locationSelectionType = tabTextList[tabIndex];
  94. }
  95. }