controller.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../../../../common/models/enums/index.dart';
  4. import '../../../../common/services/index.dart';
  5. import '../../../../common/style/index.dart';
  6. import '../../../../common/utils/index.dart';
  7. class SkinController extends GetxController {
  8. SkinController();
  9. Rx<ThemeMode> themeMode = Rx<ThemeMode>(ThemeMode.system);
  10. Rx<O2Skin> skin = O2Skin.blue.obs;
  11. _initData() {
  12. update(["skin"]);
  13. }
  14. @override
  15. void onReady() {
  16. loadThemeMode();
  17. super.onReady();
  18. }
  19. Future<void> loadThemeMode() async {
  20. themeMode.value = SharedPreferenceService.to.getThemeMode();
  21. OLogger.d('当前主题mode :${themeMode.value.name}');
  22. skin.value = SharedPreferenceService.to.getThemeSkin();
  23. OLogger.d('当前系统主题皮肤: ${skin.value.name}');
  24. _initData();
  25. }
  26. Future<void> changeWithSystem(bool value) async {
  27. OLogger.d('切换跟随系统,$value');
  28. if (value) {
  29. themeMode.value = ThemeMode.system; // 默认跟随系统
  30. } else {
  31. themeMode.value = ThemeMode.light;
  32. }
  33. _initData();
  34. updateUI();
  35. }
  36. Future<void> clickLightMode() async {
  37. if (themeMode.value == ThemeMode.light) {
  38. return;
  39. }
  40. themeMode.value = ThemeMode.light;
  41. _initData();
  42. updateUI();
  43. }
  44. Future<void> clickDarkMode() async {
  45. if (themeMode.value == ThemeMode.dark) {
  46. return;
  47. }
  48. themeMode.value = ThemeMode.dark;
  49. _initData();
  50. updateUI();
  51. }
  52. Future<void> updateUI() async {
  53. await SharedPreferenceService.to.putString(SharedPreferenceService.appThemeModeKey, themeMode.value.name);
  54. Get.changeThemeMode(themeMode.value);
  55. OLogger.d('切换模式');
  56. }
  57. /// 点击切换主题皮肤
  58. clickChangeSkin(O2Skin s) {
  59. if (skin.value == s) {
  60. return;
  61. }
  62. skin.value = s;
  63. _initData();
  64. updateSkin();
  65. }
  66. /// 切换主题皮肤
  67. Future<void> updateSkin() async {
  68. await SharedPreferenceService.to.putString(SharedPreferenceService.appThemeSkinKey, skin.value.name);
  69. final theme = Get.isDarkMode ? AppTheme.getDarkThemeBySkin(skin.value) : AppTheme.getLightThemeBySkin(skin.value);
  70. Get.changeTheme(theme);
  71. }
  72. }