GETX主题管理

发布于 2025-01-27 00:36:10 字数 904 浏览 1 评论 0原文

我正在尝试在我的应用程序中实现主题管理。

在我拥有的GetMaterialApp中:

themeMode:  ThemeService().getThemeMode(),

在主题服务类中,我有:

  ThemeMode getThemeMode() {
    String theme = getSavedTheme();
    print('Loading theme: $theme');

    switch (theme) {
      case 'dark':
        return ThemeMode.dark;
      case 'system':
        return ThemeMode.system;
      default:
        return ThemeMode.light;
    }
  }

  String getSavedTheme() {
    var value = _getStorage.read(storageKey);
    print('Loaded: $value');
    return value ?? 'light';
  }

第一次打开应用程序时,该应用程序总是在Light主题中加载,并且我在控制台中获得以下输出:

I/flutter ( 4252): Loaded: null
I/flutter ( 4252): Loading theme: light

似乎GetX存储没有加载值来自存储的prefs。手动检查文件时,该值在prefs中是正确的:

{"savedTheme":"system"}

如果我首次加载应用程序后立即重新启动,它可以正常工作并更改应用程序主题。

请问我可能做错了什么?

I am trying to implement theme management in my app.

In the GetMaterialApp I have:

themeMode:  ThemeService().getThemeMode(),

In the ThemeService class I have:

  ThemeMode getThemeMode() {
    String theme = getSavedTheme();
    print('Loading theme: $theme');

    switch (theme) {
      case 'dark':
        return ThemeMode.dark;
      case 'system':
        return ThemeMode.system;
      default:
        return ThemeMode.light;
    }
  }

  String getSavedTheme() {
    var value = _getStorage.read(storageKey);
    print('Loaded: $value');
    return value ?? 'light';
  }

When I open the app for the first time, the app always loads in light theme and I get the following output in the console:

I/flutter ( 4252): Loaded: null
I/flutter ( 4252): Loading theme: light

Seems like Getx Storage isn't loading the value from the stored prefs. Checking the file manually, the value is correct in the prefs:

{"savedTheme":"system"}

If I hot restart right after loading the app for the first time, it works properly and changes the app theme.

Any ideas on what I might be doing wrong, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2025-02-03 00:36:10

请检查主要方法的GetStorage初始化

void main() async { // make it async
  await GetStorage.init(); // add this
  runApp(MyApp());
}

Please check GetStorage initialization in Main method

void main() async { // make it async
  await GetStorage.init(); // add this
  runApp(MyApp());
}
浪漫人生路 2025-02-03 00:36:10

替换

await GetStorage.init();

解决我的问题的解决方案是为了完整的缘故

await GetStorage.init('theme');

,这就是我在主题服务类中所拥有的

class ThemeService {
  final _getStorage = GetStorage('theme');
  final themeBrightness = 'savedTheme';
  final themeColor = 'themeColor';

  ThemeMode getThemeMode() {
    String theme = getSavedTheme();
    print('Loading theme: $theme');

    switch (theme) {
      case 'dark':
        return ThemeMode.dark;
      case 'system':
        return ThemeMode.system;
      default:
        return ThemeMode.light;
    }
  }

  String getSavedTheme() {
    var value = _getStorage.read(themeBrightness);
    print('Loaded: $value');
    return value ?? 'light';
  }
}

The solution to my problem was to replace

await GetStorage.init();

with

await GetStorage.init('theme');

For completness sake, this is what I had in the ThemeService class

class ThemeService {
  final _getStorage = GetStorage('theme');
  final themeBrightness = 'savedTheme';
  final themeColor = 'themeColor';

  ThemeMode getThemeMode() {
    String theme = getSavedTheme();
    print('Loading theme: $theme');

    switch (theme) {
      case 'dark':
        return ThemeMode.dark;
      case 'system':
        return ThemeMode.system;
      default:
        return ThemeMode.light;
    }
  }

  String getSavedTheme() {
    var value = _getStorage.read(themeBrightness);
    print('Loaded: $value');
    return value ?? 'light';
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文