Files
flutter-picgo/lib/model/theme_state.dart
2020-07-02 13:26:55 +08:00

55 lines
1.2 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:flutter_picgo/utils/shared_preferences.dart';
class ThemeState with ChangeNotifier {
ThemeMode _mode;
ThemeMode get currentMode => _mode;
static const Map<ThemeMode, String> modeMap = {
ThemeMode.light: '浅色模式',
ThemeMode.dark: '深色模式',
ThemeMode.system: '跟随系统'
};
ThemeState() {
_init();
}
_init() async {
var sp = await SpUtil.getInstance();
int localMode = sp.getInt(SharedPreferencesKeys.localThemeState) ?? 2;
changeThemeState(_parseState(localMode));
}
changeThemeState(ThemeMode mode) async {
_mode = mode;
notifyListeners();
// save state
var sp = await SpUtil.getInstance();
sp.putInt(SharedPreferencesKeys.localThemeState, _parseInt(mode));
}
/// num转enum
/// 0浅色模式 1深色模式 2跟随系统
int _parseInt(ThemeMode s) {
if (s == ThemeMode.light) {
return 0;
} else if (s == ThemeMode.dark) {
return 1;
} else {
return 2;
}
}
/// enmu转num
ThemeMode _parseState(int mode) {
if (mode == 0) {
return ThemeMode.light;
} else if (mode == 1) {
return ThemeMode.dark;
} else {
return ThemeMode.system;
}
}
}