我可以在我的应用程序中更改主题,但我在使用
Get.isDarkMode
时遇到问题,当我尝试更改 text color
或我尝试更改 icon
时,它不听。
import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:get/get.dart';
class ThemeService {
final _box = GetStorage();
final _key = 'isDarkMode';
ThemeMode get theme => _loadThemeFromBox() ? ThemeMode.dark : ThemeMode.light;
bool _loadThemeFromBox() => _box.read(_key) ?? false;
_saveThemeToBox(bool isDarkMode) => _box.write(_key, isDarkMode);
void switchTheme() {
Get.changeThemeMode(_loadThemeFromBox() ? ThemeMode.light : ThemeMode.dark);
_saveThemeToBox(!_loadThemeFromBox());
}
}
IconButton( icon: Icon(Get.isDarkMode ? Icons.dark_mode_outlined : Icons.light_mode_outlined) , onPressed: (){
ThemeService().switchTheme();
},)