| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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/repository/config_repository.dart';
- import 'package:keyboard/utils/atmob_log.dart';
- import '../../data/bean/character_group_info.dart';
- import '../../data/repository/characters_repository.dart';
- @injectable
- class CharacterController extends BaseController
- with GetTickerProviderStateMixin {
- final String tag = "CharacterController";
- final CharactersRepository charactersRepository;
- final ConfigRepository configRepository;
- CharacterController(this.charactersRepository,this.configRepository);
- RxList<CharacterGroupInfo> get characterGroupList =>
- charactersRepository.characterGroupList;
- late Rx<TabController> tabController;
- late PageController pageController;
- RxInt currentTabBarIndex = 0.obs;
- RxList<String> keyboardOptions = ["默认键盘", "新建键盘1"].obs;
- Rx<String?> selectedValue = Rx<String?>(null);
- @override
- void onInit() {
- super.onInit();
- _dataLoad();
- }
- void _dataLoad() async {
- charactersRepository.getCharactersGroup();
- pageController = PageController();
- tabController =
- TabController(
- length: characterGroupList.length,
- vsync: this,
- initialIndex: 0,
- ).obs;
- ever(charactersRepository.characterGroupList, (value) {
- AtmobLog.d(tag, "characterGroupList changed");
- if (value.isNotEmpty) {
- tabController.value = TabController(
- length: characterGroupList.length,
- vsync: this,
- initialIndex: 0,
- );
- }
- });
- }
- void onTabChanged(int index) {
- if (index >= characterGroupList.length) {
- return;
- }
- AtmobLog.d(tag, "onTabChanged index:$index");
- pageController.animateToPage(
- index,
- duration: const Duration(milliseconds: 300),
- curve: Curves.easeInToLinear,
- );
- }
- void onPageChanged(int index) {
- if (index >= characterGroupList.length) {
- return;
- }
- AtmobLog.d(tag, "onPageChanged index:$index");
- currentTabBarIndex.value = index;
- tabController.value.animateTo(
- index,
- duration: const Duration(milliseconds: 300),
- );
- }
- @override
- void onReady() {
- super.onReady();
- }
- @override
- void onClose() {
- pageController.dispose();
- tabController.value.dispose();
- super.onClose();
- }
- clickMyKeyboard() {
- AtmobLog.d(tag, "clickMyKeyboard");
- }
- void updateSelectedValue(String? newValue) {
- selectedValue.value = newValue;
- }
- // 添加一个新的键盘
- void addKeyboard(String name) {
- keyboardOptions.add(name);
- }
- // 删除某个键盘
- void removeKeyboard(String name) {
- keyboardOptions.remove(name);
- // 如果删除的是当前选中的值,则重置
- if (selectedValue.value == name) {
- selectedValue.value =
- keyboardOptions.isNotEmpty ? keyboardOptions.first : null;
- }
- }
- }
|