veilidchat/lib/theming/theme_service.dart
Christien Rioux fe9d9f8aca lint work
2023-07-26 15:58:38 -04:00

55 lines
1.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'themes/themes.dart';
class ThemeService {
ThemeService._();
static late SharedPreferences prefs;
static ThemeService? _instance;
static Future<ThemeService> get instance async {
if (_instance == null) {
prefs = await SharedPreferences.getInstance();
_instance = ThemeService._();
}
return _instance!;
}
final allThemes = <String, ThemeData>{
'dark': darkTheme,
'light': lightTheme,
};
String get previousThemeName {
var themeName = prefs.getString('previousThemeName');
if (themeName == null) {
final isPlatformDark =
WidgetsBinding.instance.platformDispatcher.platformBrightness ==
Brightness.dark;
themeName = isPlatformDark ? 'dark' : 'light';
}
return themeName;
}
ThemeData get initial {
var themeName = prefs.getString('theme');
if (themeName == null) {
final isPlatformDark =
WidgetsBinding.instance.platformDispatcher.platformBrightness ==
Brightness.dark;
themeName = isPlatformDark ? 'dark' : 'light';
}
return allThemes[themeName] ?? allThemes['light']!;
}
Future<void> save(String newThemeName) async {
final currentThemeName = prefs.getString('theme');
if (currentThemeName != null) {
await prefs.setString('previousThemeName', currentThemeName);
}
await prefs.setString('theme', newThemeName);
}
ThemeData getByName(String name) => allThemes[name]!;
}