node_controller.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. void updateGroups(Groups? groups) {
  36. this.groups = groups;
  37. _updateTabList();
  38. }
  39. /// 更新 Tab 列表
  40. void _updateTabList() {
  41. final tabs = <String>[];
  42. // 添加 Normal tab
  43. if (groups?.normal != null && groups!.normal!.list?.isNotEmpty == true) {
  44. tabs.add('All');
  45. }
  46. // 添加 Streaming tab
  47. if (groups?.streaming != null &&
  48. groups!.streaming!.list?.isNotEmpty == true) {
  49. tabs.add('Streaming');
  50. }
  51. tabTextList = tabs;
  52. }
  53. /// 根据 tab 索引获取对应的数据
  54. Normal? getDataByTabIndex(int tabIndex) {
  55. if (tabIndex == 0) {
  56. return groups?.normal;
  57. } else if (tabIndex == 1) {
  58. return groups?.streaming;
  59. }
  60. return null;
  61. }
  62. IconData getTabIcon(int tabIndex) {
  63. if (tabIndex == 0) {
  64. return IconFont.icon24;
  65. } else if (tabIndex == 1) {
  66. return IconFont.icon25;
  67. }
  68. return IconFont.icon24;
  69. }
  70. /// 获取国家的展开状态
  71. bool getExpandedState(int tabIndex, String countryCode) {
  72. final key = '${tabIndex}_$countryCode';
  73. return expandedStates[key] ?? false; // 默认收缩
  74. }
  75. /// 设置国家的展开状态
  76. void setExpandedState(int tabIndex, String countryCode, bool expanded) {
  77. final key = '${tabIndex}_$countryCode';
  78. expandedStates[key] = expanded;
  79. }
  80. }