node_controller.dart 2.3 KB

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