import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:injectable/injectable.dart'; import 'package:keyboard/base/base_controller.dart'; import 'package:keyboard/data/bean/keyboard_info.dart'; import 'package:keyboard/data/repository/account_repository.dart'; import 'package:keyboard/data/repository/config_repository.dart'; import 'package:keyboard/module/keyboard_manage/keyboard_manage_page.dart'; import 'package:keyboard/utils/atmob_log.dart'; import '../../data/api/response/user_info_response.dart'; import '../../data/bean/character_group_info.dart'; import '../../data/consts/event_report.dart'; import '../../data/repository/characters_repository.dart'; import '../../data/repository/keyboard_repository.dart'; import '../../handler/event_handler.dart'; import '../character_custom/character_custom_page.dart'; @injectable class CharacterController extends BaseController with GetTickerProviderStateMixin { final String tag = "CharacterController"; final CharactersRepository charactersRepository; final ConfigRepository configRepository; final KeyboardRepository keyboardRepository; final AccountRepository accountRepository; Rxn get userInfo => accountRepository.userInfo; CharacterController( this.accountRepository, this.charactersRepository, this.configRepository, this.keyboardRepository, ); // 人设主题 List get characterGroupList => charactersRepository.characterGroupList; // 键盘列表 RxList get keyboardInfoList => keyboardRepository.keyboardInfoList; Rx tabController = Rx(null); PageController pageController = PageController(); RxInt currentTabBarIndex = 0.obs; Rx currentCharacterGroupInfo = CharacterGroupInfo().obs; Rx currentKeyboardInfo = KeyboardInfo().obs; @override void onInit() { super.onInit(); if (characterGroupList.isNotEmpty) { currentCharacterGroupInfo.value = characterGroupList.first; } if (keyboardInfoList.isNotEmpty) { currentKeyboardInfo.value = keyboardInfoList.firstWhere( (element) => element.isChoose == true, orElse: () => keyboardInfoList.first, ); } _dataLoad(); } void _dataLoad() async { AtmobLog.d(tag, "_dataLoad"); tabController.value = TabController( length: characterGroupList.length, vsync: this, initialIndex: 0, ); debugPrint("qqq tabController: ${tabController.hashCode}"); // ever(charactersRepository.characterGroupList, (value) { // AtmobLog.d(tag, "characterGroupList changed"); // if (value.isNotEmpty) { // tabController.value?.dispose(); // tabController.value = TabController( // length: characterGroupList.length, // vsync: this, // initialIndex: 0, // ); // currentCharacterGroupInfo.value = characterGroupList.first; // AtmobLog.d( // tag, // "currentCharacterGroupInfo.value: ${characterGroupList.first.id}", // ); // } // }); ever(keyboardInfoList, (value) { AtmobLog.d(tag, "keyboardInfoList1 changed ${keyboardInfoList.length}"); if (value.isNotEmpty) { currentKeyboardInfo.value = keyboardInfoList.first; print("currentKeyboardInfo.value: ${currentKeyboardInfo.value}"); } }); } void onTabChanged(int index) { if (index >= characterGroupList.length) { return; } pageController.animateToPage( index, duration: const Duration(milliseconds: 300), curve: Curves.easeInToLinear, ); } void onPageChanged(int index) { if (index >= characterGroupList.length) { return; } currentTabBarIndex.value = index; currentCharacterGroupInfo.value = characterGroupList[index]; tabController.value?.animateTo( index, duration: const Duration(milliseconds: 300), ); } @override void onReady() { super.onReady(); EventHandler.report(EventId.event_00001); } @override void onClose() { pageController.dispose(); tabController.value?.dispose(); super.onClose(); } void clickMyKeyboard() { AtmobLog.d(tag, "clickMyKeyboard"); KeyboardManagePage.start(); } void clickCustomCharacter() { AtmobLog.d(tag, "clickCustomCharacter"); CharacterCustomPage.start(); } // 切换键盘 void switchKeyboard(String? newValue) { currentKeyboardInfo.value = keyboardInfoList.firstWhere( (element) => element.name == newValue, orElse: () => keyboardInfoList.first, ); } }