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 get instance async { if (_instance == null) { prefs = await SharedPreferences.getInstance(); _instance = ThemeService._(); } return _instance!; } final allThemes = { 'dark': darkTheme, 'light': lightTheme, }; String get previousThemeName { String? themeName = prefs.getString('previousThemeName'); if (themeName == null) { final isPlatformDark = WidgetsBinding.instance.window.platformBrightness == Brightness.dark; themeName = isPlatformDark ? 'light' : 'dark'; } return themeName; } get initial { String? themeName = prefs.getString('theme'); if (themeName == null) { final isPlatformDark = WidgetsBinding.instance.window.platformBrightness == Brightness.dark; themeName = isPlatformDark ? 'dark' : 'light'; } return allThemes[themeName]; } save(String newThemeName) { var currentThemeName = prefs.getString('theme'); if (currentThemeName != null) { prefs.setString('previousThemeName', currentThemeName); } prefs.setString('theme', newThemeName); } ThemeData getByName(String name) { return allThemes[name]!; } }