2024-10-06 11:29:47 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-07-31 14:48:22 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2024-10-06 11:29:47 +00:00
|
|
|
import 'package:solian/models/theme.dart';
|
2024-07-31 14:48:22 +00:00
|
|
|
import 'package:solian/theme.dart';
|
|
|
|
|
|
|
|
class ThemeSwitcher extends ChangeNotifier {
|
|
|
|
ThemeData lightThemeData;
|
|
|
|
ThemeData darkThemeData;
|
|
|
|
|
|
|
|
ThemeSwitcher({
|
|
|
|
required this.lightThemeData,
|
|
|
|
required this.darkThemeData,
|
|
|
|
});
|
|
|
|
|
|
|
|
Future<void> restoreTheme() async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
2024-10-06 11:29:47 +00:00
|
|
|
if (prefs.containsKey('global_theme')) {
|
|
|
|
final value = SolianThemeData.fromJson(
|
|
|
|
jsonDecode(prefs.getString('global_theme')!),
|
|
|
|
);
|
|
|
|
final agedTheme = prefs.getBool('aged_theme');
|
|
|
|
lightThemeData = AppTheme.buildFromData(
|
|
|
|
Brightness.light,
|
|
|
|
value,
|
|
|
|
useMaterial3: agedTheme == null ? true : !agedTheme,
|
|
|
|
);
|
|
|
|
darkThemeData = AppTheme.buildFromData(
|
|
|
|
Brightness.dark,
|
|
|
|
value,
|
|
|
|
useMaterial3: agedTheme == null ? true : !agedTheme,
|
|
|
|
);
|
2024-07-31 14:48:22 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTheme(ThemeData light, dark) {
|
|
|
|
lightThemeData = light;
|
|
|
|
darkThemeData = dark;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2024-10-06 11:29:47 +00:00
|
|
|
|
|
|
|
Future<void> setThemeData(SolianThemeData? data) async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
if (data == null) {
|
|
|
|
prefs.remove('global_theme');
|
|
|
|
} else {
|
|
|
|
prefs.setString(
|
|
|
|
'global_theme',
|
|
|
|
jsonEncode(data.toJson()),
|
|
|
|
);
|
|
|
|
lightThemeData = AppTheme.buildFromData(Brightness.light, data);
|
|
|
|
darkThemeData = AppTheme.buildFromData(Brightness.dark, data);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setAgedTheme(bool enabled) async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
prefs.setBool('aged_theme', enabled);
|
|
|
|
await restoreTheme();
|
|
|
|
}
|
2024-07-31 14:48:22 +00:00
|
|
|
}
|