Simplify code, remove html dependency
This commit removes the `html' package dependency, speeding up the compile time of the Dart code. It also simplifies the code and removes some unused code. For instance the sun, moon, and lightbulb icons is removed, the `Theme' class is removed, and the `switchTheme' function is now merged into the `setTheme' function. The `makeThemeItem' function has also had its second argument removed.
This commit is contained in:
@@ -1,53 +1,23 @@
|
||||
import 'dart:html';
|
||||
|
||||
class Theme {
|
||||
String _name;
|
||||
String _icon;
|
||||
|
||||
Theme(String t_name, String t_icon) {
|
||||
_name = t_name;
|
||||
_icon = t_icon;
|
||||
}
|
||||
|
||||
String getIcon() => _icon;
|
||||
String getName() => _name;
|
||||
}
|
||||
|
||||
final themes = {
|
||||
'light': Theme('light', 'fa-sun'),
|
||||
'dark': Theme('dark', 'fa-lightbulb'),
|
||||
'black': Theme('black', 'fa-moon')
|
||||
};
|
||||
|
||||
final localStorage = window.localStorage;
|
||||
|
||||
Future<void> setTheme([String theme]) async {
|
||||
final currentTheme = theme ?? localStorage['theme'] ??
|
||||
() {
|
||||
final devicePrefersDark =
|
||||
window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
return devicePrefersDark ? 'dark' : 'light';
|
||||
}();
|
||||
localStorage['theme'] = currentTheme;
|
||||
querySelector('body')
|
||||
..classes.clear()
|
||||
..classes.add(currentTheme);
|
||||
}
|
||||
|
||||
void enableThemeChanger() {
|
||||
final darkBtn = querySelector('#darkBtn');
|
||||
final lightBtn = querySelector('#lightBtn');
|
||||
final blackBtn = querySelector('#blackBtn');
|
||||
|
||||
lightBtn.onClick.listen((_) => switchTheme(themes['light']));
|
||||
darkBtn.onClick.listen((_) => switchTheme(themes['dark']));
|
||||
blackBtn.onClick.listen((_) => switchTheme(themes['black']));
|
||||
}
|
||||
|
||||
Future<void> setTheme() async {
|
||||
final currentTheme = themes[localStorage['theme']] ??
|
||||
() {
|
||||
final devicePrefersDark =
|
||||
window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
return themes[devicePrefersDark ? 'dark' : 'light'];
|
||||
}();
|
||||
querySelector('body')
|
||||
..classes.clear()
|
||||
..classes.add(currentTheme.getName());
|
||||
}
|
||||
|
||||
void switchTheme(Theme currentTheme) {
|
||||
// Set HTML theme
|
||||
querySelector('body')
|
||||
..classes.clear()
|
||||
..classes.add(currentTheme.getName());
|
||||
// Set storage theme
|
||||
localStorage['theme'] = currentTheme.getName();
|
||||
final themes = <String>['light', 'dark', 'black'];
|
||||
themes.forEach((theme) =>
|
||||
querySelector('#${theme}Btn').onClick.listen((_) => setTheme(theme))
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user