2023-03-25 14:29:28 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-03-29 15:14:27 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-03-29 18:02:00 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-03-25 14:29:28 +01:00
|
|
|
|
|
|
|
class AppTheme {
|
|
|
|
ThemeMode mode;
|
|
|
|
|
|
|
|
AppTheme(this.mode);
|
|
|
|
|
2023-03-29 15:14:27 +02:00
|
|
|
String name(BuildContext context) {
|
|
|
|
final trans = AppLocalizations.of(context);
|
|
|
|
|
|
|
|
if (mode == ThemeMode.light) {
|
|
|
|
return trans!.themeLight;
|
2023-03-25 14:29:28 +01:00
|
|
|
}
|
|
|
|
if (mode == ThemeMode.dark) {
|
2023-03-29 15:14:27 +02:00
|
|
|
return trans!.themeDark;
|
2023-03-25 14:29:28 +01:00
|
|
|
}
|
|
|
|
|
2023-03-29 15:14:27 +02:00
|
|
|
return trans!.themeSystem;
|
2023-03-25 14:29:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-03-29 18:02:00 +02:00
|
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.setInt('theme', mode.index);
|
2023-03-25 14:29:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Future<AppTheme> fromDisk() async {
|
2023-03-29 18:02:00 +02:00
|
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
return AppTheme(ThemeMode.values[(prefs.getInt('theme')) ?? 0]);
|
2023-03-25 14:29:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(other) {
|
|
|
|
if (other.runtimeType == runtimeType) {
|
|
|
|
if (other.hashCode == hashCode) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode => mode.index;
|
|
|
|
}
|