1af8d6f068
(only for user, server and theme) This was done, because localstore is somewhat inconsistent in terms of events on different platforms. Also storing user, server and theme using shared-preferences should fit into flutters ecosystem a little better
73 lines
1.5 KiB
Dart
73 lines
1.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class AppTheme {
|
|
ThemeMode mode;
|
|
|
|
AppTheme(this.mode);
|
|
|
|
String name(BuildContext context) {
|
|
final trans = AppLocalizations.of(context);
|
|
|
|
if (mode == ThemeMode.light) {
|
|
return trans!.themeLight;
|
|
}
|
|
if (mode == ThemeMode.dark) {
|
|
return trans!.themeDark;
|
|
}
|
|
|
|
return trans!.themeSystem;
|
|
}
|
|
|
|
IconData get icon {
|
|
if (mode == ThemeMode.light) {
|
|
return Icons.light_mode;
|
|
}
|
|
if (mode == ThemeMode.dark) {
|
|
return Icons.dark_mode;
|
|
}
|
|
|
|
return Icons.brightness_auto;
|
|
}
|
|
|
|
static get auto {
|
|
return AppTheme(ThemeMode.system);
|
|
}
|
|
|
|
static get light {
|
|
return AppTheme(ThemeMode.light);
|
|
}
|
|
|
|
static get dark {
|
|
return AppTheme(ThemeMode.dark);
|
|
}
|
|
|
|
static List<AppTheme> list() {
|
|
return [AppTheme.auto, AppTheme.light, AppTheme.dark];
|
|
}
|
|
|
|
Future<void> toDisk() async {
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
await prefs.setInt('theme', mode.index);
|
|
}
|
|
|
|
static Future<AppTheme> fromDisk() async {
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
return AppTheme(ThemeMode.values[(prefs.getInt('theme')) ?? 0]);
|
|
}
|
|
|
|
@override
|
|
bool operator ==(other) {
|
|
if (other.runtimeType == runtimeType) {
|
|
if (other.hashCode == hashCode) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
@override
|
|
int get hashCode => mode.index;
|
|
}
|