character_controller.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:keyboard/base/base_controller.dart';
  5. import 'package:keyboard/data/bean/keyboard_info.dart';
  6. import 'package:keyboard/data/repository/config_repository.dart';
  7. import 'package:keyboard/utils/atmob_log.dart';
  8. import '../../data/bean/character_group_info.dart';
  9. import '../../data/repository/characters_repository.dart';
  10. import '../../data/repository/keyboard_repository.dart';
  11. @injectable
  12. class CharacterController extends BaseController
  13. with GetTickerProviderStateMixin {
  14. final String tag = "CharacterController";
  15. final CharactersRepository charactersRepository;
  16. final ConfigRepository configRepository;
  17. final KeyboardRepository keyboardRepository;
  18. CharacterController(
  19. this.charactersRepository,
  20. this.configRepository,
  21. this.keyboardRepository,
  22. );
  23. // 人设主题
  24. RxList<CharacterGroupInfo> get characterGroupList =>
  25. charactersRepository.characterGroupList;
  26. // 键盘列表
  27. RxList<KeyboardInfo> get keyboardInfoList =>
  28. keyboardRepository.keyboardInfoList;
  29. late Rx<TabController> tabController;
  30. late PageController pageController;
  31. RxInt currentTabBarIndex = 0.obs;
  32. Rx<CharacterGroupInfo> currentCharacterGroupInfo = CharacterGroupInfo().obs;
  33. Rx<KeyboardInfo> currentKeyboardInfo = KeyboardInfo().obs;
  34. @override
  35. void onInit() {
  36. super.onInit();
  37. _dataLoad();
  38. }
  39. void _dataLoad() async {
  40. pageController = PageController();
  41. tabController =
  42. TabController(
  43. length: characterGroupList.length,
  44. vsync: this,
  45. initialIndex: 0,
  46. ).obs;
  47. ever(charactersRepository.characterGroupList, (value) {
  48. AtmobLog.d(tag, "characterGroupList changed");
  49. if (value.isNotEmpty) {
  50. tabController.value.dispose();
  51. tabController.value = TabController(
  52. length: characterGroupList.length,
  53. vsync: this,
  54. initialIndex: 0,
  55. );
  56. currentCharacterGroupInfo.value = characterGroupList.first;
  57. AtmobLog.d(
  58. tag,
  59. "currentCharacterGroupInfo.value: ${characterGroupList.first.id}",
  60. );
  61. }
  62. });
  63. ever(keyboardRepository.keyboardInfoList, (value) {
  64. AtmobLog.d(tag, "keyboardInfoList1 changed");
  65. if (value.isNotEmpty) {
  66. currentKeyboardInfo.value = keyboardInfoList.first;
  67. print("currentKeyboardInfo.value: ${currentKeyboardInfo.value}");
  68. }
  69. });
  70. }
  71. void onTabChanged(int index) {
  72. if (index >= characterGroupList.length) {
  73. return;
  74. }
  75. pageController.animateToPage(
  76. index,
  77. duration: const Duration(milliseconds: 300),
  78. curve: Curves.easeInToLinear,
  79. );
  80. }
  81. void onPageChanged(int index) {
  82. if (index >= characterGroupList.length) {
  83. return;
  84. }
  85. currentTabBarIndex.value = index;
  86. currentCharacterGroupInfo.value = characterGroupList[index];
  87. tabController.value.animateTo(
  88. index,
  89. duration: const Duration(milliseconds: 300),
  90. );
  91. }
  92. @override
  93. void onReady() {
  94. super.onReady();
  95. }
  96. @override
  97. void onClose() {
  98. pageController.dispose();
  99. tabController.value.dispose();
  100. super.onClose();
  101. }
  102. clickMyKeyboard() {
  103. AtmobLog.d(tag, "clickMyKeyboard");
  104. }
  105. void updateSelectedValue(String? newValue) {
  106. currentKeyboardInfo.value = keyboardInfoList.firstWhere(
  107. (element) => element.name == newValue,
  108. orElse: () => keyboardInfoList.first,
  109. );
  110. }
  111. }