-
Notifications
You must be signed in to change notification settings - Fork 32
/
common_themes.dart
361 lines (333 loc) · 11.5 KB
/
common_themes.dart
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yaru/src/text/text_theme.dart';
import 'package:yaru/src/themes/constants.dart';
import 'package:yaru/src/themes/page_transitions.dart';
import 'package:yaru_colors/yaru_colors.dart';
// AppBar
AppBarTheme _createLightAppBar(ColorScheme colorScheme) {
return AppBarTheme(
shadowColor: colorScheme.onSurface,
scrolledUnderElevation: kAppBarElevation,
surfaceTintColor: colorScheme.background,
toolbarHeight: kAppBarHeight,
elevation: kAppBarElevation,
systemOverlayStyle: SystemUiOverlayStyle.light,
backgroundColor: colorScheme.background,
foregroundColor: colorScheme.onSurface,
titleTextStyle: textTheme.headline6!.copyWith(
color: colorScheme.onSurface,
fontWeight: FontWeight.normal,
),
iconTheme: IconThemeData(color: colorScheme.onSurface),
actionsIconTheme: const IconThemeData(color: YaruColors.inkstone),
);
}
AppBarTheme _createDarkAppBarTheme(ColorScheme colorScheme) {
return AppBarTheme(
shadowColor: colorScheme.background,
scrolledUnderElevation: kAppBarElevation,
surfaceTintColor: colorScheme.background,
toolbarHeight: kAppBarHeight,
elevation: kAppBarElevation,
systemOverlayStyle: SystemUiOverlayStyle.dark,
backgroundColor: colorScheme.surface,
foregroundColor: colorScheme.onSurface,
titleTextStyle: textTheme.headline6!.copyWith(
color: colorScheme.onSurface,
fontWeight: FontWeight.normal,
),
);
}
// TextField
final inputDecorationTheme = InputDecorationTheme(
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(kButtonRadius)),
isDense: true,
);
TextSelectionThemeData _createTextSelectionTheme(ColorScheme colorScheme) {
return TextSelectionThemeData(
cursorColor: colorScheme.primary,
selectionColor: colorScheme.primary.withOpacity(0.40),
);
}
// Buttons
const _commonButtonStyle = ButtonStyle(visualDensity: VisualDensity.standard);
final _buttonThemeData = ButtonThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kButtonRadius),
),
);
final _outlinedButtonThemeData = OutlinedButtonThemeData(
style: OutlinedButton.styleFrom(
side: const BorderSide(color: Colors.black38, width: 0.3),
visualDensity: _commonButtonStyle.visualDensity,
foregroundColor: YaruColors.textGrey,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kButtonRadius),
),
),
);
final _darkOutlinedButtonThemeData = OutlinedButtonThemeData(
style: OutlinedButton.styleFrom(
side: const BorderSide(color: Colors.white38, width: 0.3),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kButtonRadius),
),
visualDensity: _commonButtonStyle.visualDensity,
foregroundColor: Colors.white,
),
);
final _textButtonThemeData = TextButtonThemeData(
style: TextButton.styleFrom(
visualDensity: _commonButtonStyle.visualDensity,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kButtonRadius),
),
),
);
ElevatedButtonThemeData _getElevatedButtonThemeData(Color color) {
return ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: color,
foregroundColor:
ThemeData.estimateBrightnessForColor(color) == Brightness.light
? Colors.black
: Colors.white,
visualDensity: _commonButtonStyle.visualDensity,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kButtonRadius),
),
),
);
}
const _toggleButtonsTheme = ToggleButtonsThemeData(
borderRadius: BorderRadius.all(Radius.circular(kButtonRadius)),
);
// Dialogs
final _dialogThemeDark = DialogTheme(
backgroundColor: YaruColors.coolGrey,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kWindowRadius),
side: BorderSide(color: Colors.white.withOpacity(0.2)),
),
);
final _dialogThemeLight = DialogTheme(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kWindowRadius),
),
);
// Switches
SwitchThemeData _getSwitchThemeData(Color primaryColor, Brightness brightness) {
return SwitchThemeData(
thumbColor: MaterialStateProperty.resolveWith(
(states) => _getSwitchThumbColor(states, primaryColor, brightness),
),
trackColor: MaterialStateProperty.resolveWith(
(states) => _getSwitchTrackColor(states, primaryColor, brightness),
),
);
}
Color _getSwitchThumbColor(
Set<MaterialState> states,
Color selectedColor,
Brightness brightness,
) {
if (states.contains(MaterialState.disabled)) {
return brightness == Brightness.dark
? kDisabledGreyDark
: YaruColors.warmGrey.shade200;
} else {
if (states.contains(MaterialState.selected)) {
return selectedColor;
} else {
return brightness == Brightness.dark ? YaruColors.warmGrey : Colors.white;
}
}
}
Color _getSwitchTrackColor(
Set<MaterialState> states,
Color selectedColor,
Brightness brightness,
) {
if (states.contains(MaterialState.disabled)) {
return brightness == Brightness.dark
? kDisabledGreyDark.withAlpha(120)
: YaruColors.warmGrey.shade200;
} else {
if (states.contains(MaterialState.selected)) {
return brightness == Brightness.dark
? selectedColor.withAlpha(160)
: selectedColor.withAlpha(180);
} else {
return brightness == Brightness.dark
? YaruColors.warmGrey.withAlpha(80)
: YaruColors.warmGrey.shade300;
}
}
}
// Checks & Radios
Color _getCheckFillColor(
Set<MaterialState> states,
Color selectedColor,
Brightness brightness,
) {
if (!states.contains(MaterialState.disabled)) {
if (states.contains(MaterialState.selected)) {
return selectedColor;
}
return brightness == Brightness.dark
? YaruColors.warmGrey.shade400
: YaruColors.warmGrey;
}
return brightness == Brightness.dark
? YaruColors.warmGrey.withOpacity(0.4)
: YaruColors.warmGrey.shade300;
}
Color _getCheckColor(Set<MaterialState> states, Color color) {
if (!states.contains(MaterialState.disabled)) {
return ThemeData.estimateBrightnessForColor(color) == Brightness.light
? Colors.black
: Colors.white;
}
return YaruColors.warmGrey;
}
CheckboxThemeData _getCheckBoxThemeData(
Color primaryColor,
Brightness brightness,
) {
return CheckboxThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kCheckRadius),
),
fillColor: MaterialStateProperty.resolveWith(
(states) => _getCheckFillColor(states, primaryColor, brightness),
),
checkColor: MaterialStateProperty.resolveWith(
(states) => _getCheckColor(states, primaryColor),
),
);
}
RadioThemeData _getRadioThemeData(Color primaryColor, Brightness brightness) {
return RadioThemeData(
fillColor: MaterialStateProperty.resolveWith(
(states) => _getCheckFillColor(states, primaryColor, brightness),
),
);
}
/// Helper function to create a new Yaru light theme
ThemeData createYaruLightTheme({
required ColorScheme colorScheme,
required Color primaryColor,
Color? elevatedButtonColor,
bool? useMaterial3 = true,
}) {
return ThemeData(
pageTransitionsTheme: YaruPageTransitionsTheme.horizontal,
useMaterial3: useMaterial3,
tabBarTheme: TabBarTheme(labelColor: colorScheme.onSurface),
dialogTheme: _dialogThemeLight,
brightness: Brightness.light,
primaryColor: colorScheme.primary,
canvasColor: colorScheme.background,
scaffoldBackgroundColor: colorScheme.background,
bottomAppBarColor: colorScheme.surface,
cardColor: colorScheme.surface,
dividerColor: colorScheme.onSurface.withOpacity(0.12),
backgroundColor: colorScheme.background,
dialogBackgroundColor: colorScheme.background,
errorColor: colorScheme.error,
textTheme: textTheme,
indicatorColor: colorScheme.secondary,
applyElevationOverlayColor: false,
colorScheme: colorScheme,
buttonTheme: _buttonThemeData,
outlinedButtonTheme: _outlinedButtonThemeData,
elevatedButtonTheme:
_getElevatedButtonThemeData(elevatedButtonColor ?? primaryColor),
textButtonTheme: _textButtonThemeData,
switchTheme: _getSwitchThemeData(primaryColor, Brightness.light),
checkboxTheme: _getCheckBoxThemeData(primaryColor, Brightness.light),
radioTheme: _getRadioThemeData(primaryColor, Brightness.light),
appBarTheme: _createLightAppBar(colorScheme),
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: elevatedButtonColor ?? primaryColor,
),
bottomNavigationBarTheme: BottomNavigationBarThemeData(
selectedItemColor: colorScheme.primary,
unselectedItemColor: colorScheme.onSurface.withOpacity(0.8),
),
inputDecorationTheme: inputDecorationTheme,
toggleButtonsTheme: _toggleButtonsTheme,
textSelectionTheme: _createTextSelectionTheme(colorScheme),
popupMenuTheme: _createPopupMenuThemeData(colorScheme, Brightness.light),
);
}
/// Helper function to create a new Yaru dark theme
ThemeData createYaruDarkTheme({
required ColorScheme colorScheme,
required Color primaryColor,
Color? elevatedButtonColor,
bool? useMaterial3 = true,
}) {
return ThemeData(
pageTransitionsTheme: YaruPageTransitionsTheme.horizontal,
useMaterial3: useMaterial3,
tabBarTheme: TabBarTheme(labelColor: Colors.white.withOpacity(0.8)),
dialogTheme: _dialogThemeDark,
brightness: Brightness.dark,
primaryColor: colorScheme.primary,
canvasColor: colorScheme.background,
scaffoldBackgroundColor: colorScheme.background,
bottomAppBarColor: colorScheme.surface,
cardColor: colorScheme.surface,
dividerColor: colorScheme.onSurface.withOpacity(0.12),
backgroundColor: colorScheme.background,
dialogBackgroundColor: colorScheme.background,
errorColor: colorScheme.error,
textTheme: textTheme,
indicatorColor: colorScheme.secondary,
applyElevationOverlayColor: true,
colorScheme: colorScheme,
buttonTheme: _buttonThemeData,
textButtonTheme: _textButtonThemeData,
elevatedButtonTheme:
_getElevatedButtonThemeData(elevatedButtonColor ?? primaryColor),
outlinedButtonTheme: _darkOutlinedButtonThemeData,
switchTheme: _getSwitchThemeData(primaryColor, Brightness.dark),
checkboxTheme: _getCheckBoxThemeData(primaryColor, Brightness.dark),
radioTheme: _getRadioThemeData(primaryColor, Brightness.dark),
primaryColorDark: primaryColor,
appBarTheme: _createDarkAppBarTheme(colorScheme),
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: elevatedButtonColor ?? primaryColor,
),
bottomNavigationBarTheme: BottomNavigationBarThemeData(
selectedItemColor: colorScheme.primary,
unselectedItemColor: Colors.white.withOpacity(0.8),
),
inputDecorationTheme: inputDecorationTheme,
toggleButtonsTheme: _toggleButtonsTheme,
textSelectionTheme: _createTextSelectionTheme(colorScheme),
popupMenuTheme: _createPopupMenuThemeData(colorScheme, Brightness.dark),
);
}
PopupMenuThemeData _createPopupMenuThemeData(
ColorScheme colorScheme,
Brightness brightness,
) {
return PopupMenuThemeData(
color: brightness == Brightness.dark
? const Color.fromARGB(255, 34, 34, 34)
: Colors.white,
shape: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: colorScheme.onSurface
.withOpacity(brightness == Brightness.light ? 0.3 : 0.2),
width: 1,
),
),
);
}