character_controller.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/repository/config_repository.dart';
  6. import 'package:keyboard/utils/atmob_log.dart';
  7. import '../../data/bean/character_group_info.dart';
  8. import '../../data/repository/characters_repository.dart';
  9. @injectable
  10. class CharacterController extends BaseController
  11. with GetTickerProviderStateMixin {
  12. final String tag = "CharacterController";
  13. final CharactersRepository charactersRepository;
  14. final ConfigRepository configRepository;
  15. CharacterController(this.charactersRepository,this.configRepository);
  16. RxList<CharacterGroupInfo> get characterGroupList =>
  17. charactersRepository.characterGroupList;
  18. late Rx<TabController> tabController;
  19. late PageController pageController;
  20. RxInt currentTabBarIndex = 0.obs;
  21. RxList<String> keyboardOptions = ["默认键盘", "新建键盘1"].obs;
  22. Rx<String?> selectedValue = Rx<String?>(null);
  23. @override
  24. void onInit() {
  25. super.onInit();
  26. _dataLoad();
  27. }
  28. void _dataLoad() async {
  29. charactersRepository.getCharactersGroup();
  30. pageController = PageController();
  31. tabController =
  32. TabController(
  33. length: characterGroupList.length,
  34. vsync: this,
  35. initialIndex: 0,
  36. ).obs;
  37. ever(charactersRepository.characterGroupList, (value) {
  38. AtmobLog.d(tag, "characterGroupList changed");
  39. if (value.isNotEmpty) {
  40. tabController.value = TabController(
  41. length: characterGroupList.length,
  42. vsync: this,
  43. initialIndex: 0,
  44. );
  45. }
  46. });
  47. }
  48. void onTabChanged(int index) {
  49. if (index >= characterGroupList.length) {
  50. return;
  51. }
  52. AtmobLog.d(tag, "onTabChanged index:$index");
  53. pageController.animateToPage(
  54. index,
  55. duration: const Duration(milliseconds: 300),
  56. curve: Curves.easeInToLinear,
  57. );
  58. }
  59. void onPageChanged(int index) {
  60. if (index >= characterGroupList.length) {
  61. return;
  62. }
  63. AtmobLog.d(tag, "onPageChanged index:$index");
  64. currentTabBarIndex.value = index;
  65. tabController.value.animateTo(
  66. index,
  67. duration: const Duration(milliseconds: 300),
  68. );
  69. }
  70. @override
  71. void onReady() {
  72. super.onReady();
  73. }
  74. @override
  75. void onClose() {
  76. pageController.dispose();
  77. tabController.value.dispose();
  78. super.onClose();
  79. }
  80. clickMyKeyboard() {
  81. AtmobLog.d(tag, "clickMyKeyboard");
  82. }
  83. void updateSelectedValue(String? newValue) {
  84. selectedValue.value = newValue;
  85. }
  86. // 添加一个新的键盘
  87. void addKeyboard(String name) {
  88. keyboardOptions.add(name);
  89. }
  90. // 删除某个键盘
  91. void removeKeyboard(String name) {
  92. keyboardOptions.remove(name);
  93. // 如果删除的是当前选中的值,则重置
  94. if (selectedValue.value == name) {
  95. selectedValue.value =
  96. keyboardOptions.isNotEmpty ? keyboardOptions.first : null;
  97. }
  98. }
  99. }