-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy paththeme_switcher.dart
More file actions
141 lines (122 loc) · 3.53 KB
/
theme_switcher.dart
File metadata and controls
141 lines (122 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2025 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:jaspr/dom.dart';
import 'package:jaspr/jaspr.dart';
import 'package:universal_web/web.dart' as web;
import '../common/button.dart';
import '../common/dropdown.dart';
import '../common/material_icon.dart';
@client
final class ThemeSwitcher extends StatefulComponent {
const ThemeSwitcher();
@override
State<StatefulComponent> createState() => _ThemeSwitcherState();
}
enum _Theme {
light('Light', 'Switch to the light theme.', 'light_mode'),
dark('Dark', 'Switch to the dark theme.', 'dark_mode'),
auto('Automatic', 'Match theme to device theme.', 'night_sight_auto');
final String label;
final String description;
final String iconId;
const _Theme(this.label, this.description, this.iconId);
String get id => '$name-mode';
}
final class _ThemeSwitcherState extends State<ThemeSwitcher> {
_Theme _currentTheme = _Theme.light;
@override
void initState() {
if (kIsWeb) {
final classList = web.document.body!.classList;
// If them theme is auto, it and the result will be added as classes.
// So it should be checked for first.
if (classList.contains(_Theme.auto.id)) {
_currentTheme = _Theme.auto;
} else if (classList.contains(_Theme.dark.id)) {
_currentTheme = _Theme.dark;
} else if (classList.contains(_Theme.light.id)) {
_currentTheme = _Theme.light;
} else {
// Default to light mode if no theme is set yet.
_currentTheme = _Theme.light;
classList.add(_Theme.light.id);
}
}
super.initState();
}
void _setTheme(_Theme newTheme) {
if (newTheme == _currentTheme) return;
final classList = web.document.body!.classList;
for (final mode in _Theme.values) {
classList.remove(mode.id);
}
classList.add(newTheme.id);
if (newTheme == _Theme.auto) {
classList.add(
web.window.matchMedia('(prefers-color-scheme: dark)').matches
? _Theme.dark.id
: _Theme.light.id,
);
}
try {
web.window.localStorage.setItem('theme', newTheme.id);
} catch (e) {
if (kDebugMode) {
print('Failed to save theme preference: $e');
}
}
setState(() {
_currentTheme = newTheme;
});
}
@override
Component build(BuildContext _) {
return Dropdown(
id: 'theme-switcher',
toggle: const Button(icon: 'routine', title: 'Select a theme.'),
content: div(classes: 'dropdown-menu', [
ul(
attributes: {'role': 'listbox'},
[
for (final mode in _Theme.values)
_ThemeButtonEntry(
mode: mode,
selected: _currentTheme == mode,
setMode: _setTheme,
),
],
),
]),
);
}
}
final class _ThemeButtonEntry extends StatelessComponent {
const _ThemeButtonEntry({
required this.mode,
required this.selected,
required this.setMode,
});
final _Theme mode;
final bool selected;
final void Function(_Theme) setMode;
@override
Component build(BuildContext _) => li([
button(
events: {
'click': (_) {
setMode(mode);
},
},
attributes: {
'title': mode.description,
'aria-label': mode.description,
'aria-selected': selected.toString(),
},
[
MaterialIcon(mode.iconId),
span([.text(mode.label)]),
],
),
]);
}