Implement settings
This commit is contained in:
@@ -1,16 +1,87 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:simplecloudnotifier/state/globals.dart';
|
||||
|
||||
enum ThemeColor {
|
||||
Pink(displayStr: 'Pink', key: 'PINK', value: Colors.pink),
|
||||
Red(displayStr: 'Red', key: 'RED', value: Colors.red),
|
||||
DeepOrange(displayStr: 'Deep-Orange', key: 'DEEPORANGE', value: Colors.deepOrange),
|
||||
Orange(displayStr: 'Orange', key: 'ORANGE', value: Colors.orange),
|
||||
Amber(displayStr: 'Amber', key: 'AMBER', value: Colors.amber),
|
||||
Yellow(displayStr: 'Yellow', key: 'YELLOW', value: Colors.yellow),
|
||||
Lime(displayStr: 'Lime', key: 'LIME', value: Colors.lime),
|
||||
LightGreen(displayStr: 'Light-Green', key: 'LIGHTGREEN', value: Colors.lightGreen),
|
||||
Green(displayStr: 'Green', key: 'GREEN', value: Colors.green),
|
||||
Teal(displayStr: 'Teal', key: 'TEAL', value: Colors.teal),
|
||||
Cyan(displayStr: 'Cyan', key: 'CYAN', value: Colors.cyan),
|
||||
LightBlue(displayStr: 'Light-Blue', key: 'LIGHTBLUE', value: Colors.lightBlue),
|
||||
Blue(displayStr: 'Blue', key: 'BLUE', value: Colors.blue),
|
||||
Indigo(displayStr: 'Indigo', key: 'INDIGO', value: Colors.indigo),
|
||||
Purple(displayStr: 'Purple', key: 'PURPLE', value: Colors.purple),
|
||||
DeepPurple(displayStr: 'Deep-Purple', key: 'DEEPPURPLE', value: Colors.deepPurple),
|
||||
BlueGrey(displayStr: 'Blue-Grey', key: 'BLUEGREY', value: Colors.blueGrey),
|
||||
Brown(displayStr: 'Brown', key: 'BROWN', value: Colors.brown),
|
||||
Grey(displayStr: 'Grey', key: 'GREY', value: Colors.grey);
|
||||
|
||||
const ThemeColor({required this.displayStr, required this.key, required this.value});
|
||||
|
||||
final String displayStr;
|
||||
final String key;
|
||||
final Color value;
|
||||
|
||||
@override
|
||||
toString() => displayStr;
|
||||
|
||||
static ThemeColor? parse(String? string) {
|
||||
if (string == null) return null;
|
||||
return values.firstWhere((e) => e.key == string, orElse: null);
|
||||
}
|
||||
}
|
||||
|
||||
class AppTheme extends ChangeNotifier {
|
||||
static AppTheme? _singleton = AppTheme._internal();
|
||||
|
||||
factory AppTheme() {
|
||||
return _singleton ?? (_singleton = AppTheme._internal());
|
||||
}
|
||||
|
||||
AppTheme._internal() {}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
bool _darkmode = false;
|
||||
bool get darkMode => _darkmode;
|
||||
|
||||
ThemeColor _color = ThemeColor.Blue;
|
||||
ThemeColor get color => _color;
|
||||
|
||||
void setDarkMode(bool v) {
|
||||
_darkmode = v;
|
||||
notifyListeners();
|
||||
save();
|
||||
}
|
||||
|
||||
void switchDarkMode() {
|
||||
_darkmode = !_darkmode;
|
||||
notifyListeners();
|
||||
save();
|
||||
}
|
||||
|
||||
void setColor(ThemeColor v) {
|
||||
_color = v;
|
||||
notifyListeners();
|
||||
save();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
void load() {
|
||||
_darkmode = Globals().sharedPrefs.getBool('theme.dark') ?? _darkmode;
|
||||
_color = ThemeColor.parse(Globals().sharedPrefs.getString('theme.color')) ?? _color;
|
||||
}
|
||||
|
||||
Future<void> save() async {
|
||||
await Globals().sharedPrefs.setBool('theme.dark', _darkmode);
|
||||
await Globals().sharedPrefs.setString('theme.color', _color.key);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user