forked from flutter/devtools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththeme.dart
More file actions
603 lines (517 loc) · 19.5 KB
/
theme.dart
File metadata and controls
603 lines (517 loc) · 19.5 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright 2019 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
import 'package:flutter/material.dart';
import '../ui_utils.dart';
import 'ide_theme.dart';
// TODO(kenz): try to eliminate as many custom colors as possible, and pull
// colors only from the [lightColorScheme] and the [darkColorScheme].
/// Whether dark theme should be used as the default theme if none has been
/// explicitly set.
const useDarkThemeAsDefault = true;
/// Constructs the light or dark theme for the app taking into account
/// IDE-supplied theming.
ThemeData themeFor({
required bool isDarkTheme,
required IdeTheme ideTheme,
required ThemeData theme,
}) {
final colorTheme = isDarkTheme
? _darkTheme(ideTheme: ideTheme, theme: theme)
: _lightTheme(ideTheme: ideTheme, theme: theme);
return colorTheme.copyWith(
primaryTextTheme: theme.primaryTextTheme
.merge(colorTheme.primaryTextTheme)
.apply(fontSizeFactor: ideTheme.fontSizeFactor),
textTheme: theme.textTheme
.merge(colorTheme.textTheme)
.apply(fontSizeFactor: ideTheme.fontSizeFactor),
);
}
ThemeData _darkTheme({
required IdeTheme ideTheme,
required ThemeData theme,
}) {
final background = isValidDarkColor(ideTheme.backgroundColor)
? ideTheme.backgroundColor!
: theme.colorScheme.surface;
return _baseTheme(
theme: theme,
backgroundColor: background,
);
}
ThemeData _lightTheme({
required IdeTheme ideTheme,
required ThemeData theme,
}) {
final background = isValidLightColor(ideTheme.backgroundColor)
? ideTheme.backgroundColor!
: theme.colorScheme.surface;
return _baseTheme(
theme: theme,
backgroundColor: background,
);
}
ThemeData _baseTheme({
required ThemeData theme,
required Color backgroundColor,
}) {
// TODO(kenz): do we need to pass in the foreground color from the [IdeTheme]
// as well as the background color?
return theme.copyWith(
tabBarTheme: theme.tabBarTheme.copyWith(
tabAlignment: TabAlignment.start,
dividerColor: Colors.transparent,
labelStyle: theme.regularTextStyle,
labelPadding:
const EdgeInsets.symmetric(horizontal: defaultTabBarPadding),
),
canvasColor: backgroundColor,
scaffoldBackgroundColor: backgroundColor,
iconButtonTheme: IconButtonThemeData(
style: IconButton.styleFrom(
padding: const EdgeInsets.all(densePadding),
minimumSize: Size(defaultButtonHeight, defaultButtonHeight),
fixedSize: Size(defaultButtonHeight, defaultButtonHeight),
),
),
outlinedButtonTheme: OutlinedButtonThemeData(
style: OutlinedButton.styleFrom(
minimumSize: Size(buttonMinWidth, defaultButtonHeight),
fixedSize: Size.fromHeight(defaultButtonHeight),
foregroundColor: theme.colorScheme.onSurface,
padding: const EdgeInsets.symmetric(horizontal: denseSpacing),
),
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(densePadding),
minimumSize: Size(buttonMinWidth, defaultButtonHeight),
fixedSize: Size.fromHeight(defaultButtonHeight),
),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
minimumSize: Size(buttonMinWidth, defaultButtonHeight),
fixedSize: Size.fromHeight(defaultButtonHeight),
backgroundColor: theme.colorScheme.primary,
foregroundColor: theme.colorScheme.onPrimary,
padding: const EdgeInsets.symmetric(horizontal: denseSpacing),
),
),
menuButtonTheme: MenuButtonThemeData(
style: ButtonStyle(
textStyle: WidgetStatePropertyAll<TextStyle>(theme.regularTextStyle),
fixedSize: const WidgetStatePropertyAll<Size>(Size.fromHeight(24.0)),
),
),
dropdownMenuTheme: DropdownMenuThemeData(
textStyle: theme.regularTextStyle,
),
progressIndicatorTheme: ProgressIndicatorThemeData(
linearMinHeight: defaultLinearProgressIndicatorHeight,
),
primaryTextTheme: _devToolsTextTheme(theme, theme.primaryTextTheme),
textTheme: _devToolsTextTheme(theme, theme.textTheme),
colorScheme: theme.colorScheme.copyWith(
surface: backgroundColor,
),
);
}
TextTheme _devToolsTextTheme(ThemeData theme, TextTheme textTheme) {
return textTheme.copyWith(
displayLarge: theme.boldTextStyle.copyWith(fontSize: 24),
displayMedium: theme.boldTextStyle.copyWith(fontSize: 22),
displaySmall: theme.boldTextStyle.copyWith(fontSize: 20),
headlineLarge: theme.regularTextStyle.copyWith(
fontSize: 18,
fontWeight: FontWeight.w600,
),
headlineMedium: theme.regularTextStyle.copyWith(
fontSize: 16,
fontWeight: FontWeight.w600,
),
headlineSmall: theme.regularTextStyle.copyWith(
fontSize: 14,
fontWeight: FontWeight.w600,
),
titleLarge: theme._largeText.copyWith(fontWeight: FontWeight.w500),
titleMedium: theme.regularTextStyle.copyWith(fontWeight: FontWeight.w500),
titleSmall: theme._smallText.copyWith(fontWeight: FontWeight.w500),
bodyLarge: theme._largeText,
bodyMedium: theme.regularTextStyle,
bodySmall: theme._smallText,
labelLarge: theme._largeText,
labelMedium: theme.regularTextStyle,
labelSmall: theme._smallText,
);
}
/// Light theme color scheme generated from DevTools Figma file.
///
/// Do not manually change these values.
const lightColorScheme = ColorScheme(
brightness: Brightness.light,
primary: Color(0xFF195BB9),
onPrimary: Color(0xFFFFFFFF),
primaryContainer: Color(0xFFD8E2FF),
onPrimaryContainer: Color(0xFF001A41),
secondary: Color(0xFF575E71),
onSecondary: Color(0xFFFFFFFF),
secondaryContainer: Color(0xFFDBE2F9),
onSecondaryContainer: Color(0xFF141B2C),
tertiary: Color(0xFF815600),
onTertiary: Color(0xFFFFFFFF),
tertiaryContainer: Color(0xFFFFDDB1),
onTertiaryContainer: Color(0xFF291800),
error: Color(0xFFBA1A1A),
errorContainer: Color(0xFFFFDAD5),
onError: Color(0xFFFFFFFF),
onErrorContainer: Color(0xFF410002),
surface: Color(0xFFFFFFFF),
onSurface: Color(0xFF1B1B1F),
surfaceContainerHighest: Color(0xFFE1E2EC),
onSurfaceVariant: Color(0xFF44474F),
outline: Color(0xFF75777F),
onInverseSurface: Color(0xFFF2F0F4),
inverseSurface: Color(0xFF303033),
inversePrimary: Color(0xFFADC6FF),
shadow: Color(0xFF000000),
surfaceTint: Color(0xFF195BB9),
outlineVariant: Color(0xFFC4C6D0),
scrim: Color(0xFF000000),
);
/// Dark theme color scheme generated from DevTools Figma file.
///
/// Do not manually change these values.
const darkColorScheme = ColorScheme(
brightness: Brightness.dark,
primary: Color(0xFFADC6FF),
onPrimary: Color(0xFF002E69),
primaryContainer: Color(0xFF004494),
onPrimaryContainer: Color(0xFFD8E2FF),
secondary: Color(0xFFBFC6DC),
onSecondary: Color(0xFF293041),
secondaryContainer: Color(0xFF3F4759),
onSecondaryContainer: Color(0xFFDBE2F9),
tertiary: Color(0xFFFEBA4B),
onTertiary: Color(0xFF442B00),
tertiaryContainer: Color(0xFF624000),
onTertiaryContainer: Color(0xFFFFDDB1),
error: Color(0xFFFFB4AB),
errorContainer: Color(0xFF930009),
onError: Color(0xFF690004),
onErrorContainer: Color(0xFFFFDAD5),
surface: Color(0xFF1B1B1F),
onSurface: Color(0xFFC7C6CA),
surfaceContainerHighest: Color(0xFF44474F),
onSurfaceVariant: Color(0xFFC4C6D0),
outline: Color(0xFF8E9099),
onInverseSurface: Color(0xFF1B1B1F),
inverseSurface: Color(0xFFE3E2E6),
inversePrimary: Color(0xFF195BB9),
shadow: Color(0xFF000000),
surfaceTint: Color(0xFFADC6FF),
outlineVariant: Color(0xFF44474F),
scrim: Color(0xFF000000),
);
const searchMatchColor = Colors.yellow;
final searchMatchColorOpaque = Colors.yellow.withValues(alpha: 255 / 2);
const activeSearchMatchColor = Colors.orangeAccent;
final activeSearchMatchColorOpaque =
Colors.orangeAccent.withValues(alpha: 255 / 2);
/// Gets an alternating color to use for indexed UI elements.
Color alternatingColorForIndex(int index, ColorScheme colorScheme) {
return index % 2 == 1
? colorScheme.alternatingBackgroundColor1
: colorScheme.alternatingBackgroundColor2;
}
/// Threshold used to determine whether a colour is light/dark enough for us to
/// override the default DevTools themes with.
///
/// A value of 0.5 would result in all colours being considered light/dark, and
/// a value of 0.12 allowing around only the 12% darkest/lightest colours by
/// Flutter's luminance calculation.
/// 12% was chosen because VS Code's default light background color is #f3f3f3
/// which is a little under 11%.
const _lightDarkLuminanceThreshold = 0.12;
bool isValidDarkColor(Color? color) {
if (color == null) {
return false;
}
return color.computeLuminance() <= _lightDarkLuminanceThreshold;
}
bool isValidLightColor(Color? color) {
if (color == null) {
return false;
}
return color.computeLuminance() >= 1 - _lightDarkLuminanceThreshold;
}
// Size constants:
double get defaultToolbarHeight => scaleByFontFactor(32.0);
double get defaultHeaderHeight => scaleByFontFactor(28.0);
double get defaultButtonHeight => scaleByFontFactor(26.0);
double get defaultRowHeight => scaleByFontFactor(24.0);
double get defaultLinearProgressIndicatorHeight => scaleByFontFactor(4.0);
double get defaultLinearProgressIndicatorWidth => scaleByFontFactor(200.0);
double get buttonMinWidth => scaleByFontFactor(26.0);
const defaultIconSizeBeforeScaling = 14.0;
const defaultActionsIconSizeBeforeScaling = 18.0;
double get defaultIconSize => scaleByFontFactor(defaultIconSizeBeforeScaling);
double get actionsIconSize =>
scaleByFontFactor(defaultActionsIconSizeBeforeScaling);
double get tooltipIconSize => scaleByFontFactor(12.0);
double get tableIconSize => scaleByFontFactor(12.0);
double get defaultListItemHeight => scaleByFontFactor(24.0);
double get defaultDialogWidth => scaleByFontFactor(700.0);
const extraWideSearchFieldWidth = 600.0;
const wideSearchFieldWidth = 400.0;
const defaultSearchFieldWidth = 200.0;
double get defaultTextFieldHeight => scaleByFontFactor(26.0);
double get defaultTextFieldNumberWidth => scaleByFontFactor(100.0);
// TODO(jacobr) define a more sophisticated formula for chart height.
// The chart height does need to increase somewhat to leave room for the legend
// and tick marks but does not need to scale linearly with the font factor.
double get defaultChartHeight => scaleByFontFactor(110.0);
double get actionWidgetSize => scaleByFontFactor(48.0);
double get statusLineHeight => scaleByFontFactor(20.0);
double get inputDecorationElementHeight => scaleByFontFactor(20.0);
// Padding / spacing constants:
const extraLargeSpacing = 32.0;
const largeSpacing = 16.0;
const defaultSpacing = 12.0;
const intermediateSpacing = 10.0;
const denseSpacing = 8.0;
const defaultTabBarPadding = 14.0;
const tabBarSpacing = 8.0;
const denseRowSpacing = 6.0;
const hoverCardBorderSize = 2.0;
const borderPadding = 2.0;
const densePadding = 4.0;
const noPadding = 0.0;
const defaultScrollBarOffset = 10.0;
// Other UI related constants:
final defaultBorderRadius = BorderRadius.circular(_defaultBorderRadiusValue);
const defaultRadius = Radius.circular(_defaultBorderRadiusValue);
const _defaultBorderRadiusValue = 16.0;
const defaultElevation = 4.0;
double get smallProgressSize => scaleByFontFactor(12.0);
double get mediumProgressSize => scaleByFontFactor(24.0);
const defaultTabBarViewPhysics = NeverScrollableScrollPhysics();
// Font size constants:
const unscaledExtraLargeFontSize = 16.0;
double get largeFontSize => scaleByFontFactor(unscaledLargeFontSize);
const unscaledLargeFontSize = 14.0;
double get defaultFontSize => scaleByFontFactor(unscaledDefaultFontSize);
const unscaledDefaultFontSize = 12.0;
double get smallFontSize => scaleByFontFactor(unscaledSmallFontSize);
const unscaledSmallFontSize = 10.0;
extension DevToolsSharedColorScheme on ColorScheme {
bool get isLight => brightness == Brightness.light;
bool get isDark => brightness == Brightness.dark;
Color get warningContainer => tertiaryContainer;
Color get onWarningContainer => onTertiaryContainer;
Color get onWarningContainerLink =>
isLight ? tertiary : const Color(0xFFDF9F32);
Color get onErrorContainerLink => isLight ? error : const Color(0xFFFF897D);
Color get subtleTextColor => const Color(0xFF919094);
Color get _devtoolsLink =>
isLight ? const Color(0xFF1976D2) : Colors.lightBlueAccent;
Color get alternatingBackgroundColor1 => surface;
Color get alternatingBackgroundColor2 =>
isLight ? surface.darken(0.06) : surface.brighten(0.06);
Color get selectedRowBackgroundColor =>
isLight ? const Color(0xFFC7C6CA) : const Color(0xFF5E5E62);
Color get chartAccentColor =>
isLight ? const Color(0xFFCCCCCC) : const Color(0xFF585858);
Color get contrastTextColor => isLight ? Colors.black : Colors.white;
Color get _chartSubtleColor =>
isLight ? const Color(0xFF999999) : const Color(0xFF8A8A8A);
Color get tooltipTextColor => isLight ? Colors.white : Colors.black;
Color get activeToggleButtonColor => primary.withValues(alpha: 0.3);
Color get semiTransparentOverlayColor => isLight
? Colors.grey.shade200.withAlpha(200)
: Colors.grey.shade800.withAlpha(200);
}
/// Utility extension methods to the [ThemeData] class.
extension ThemeDataExtension on ThemeData {
/// Returns whether we are currently using a dark theme.
bool get isDarkTheme => brightness == Brightness.dark;
TextStyle get regularTextStyle => fixBlurryText(
TextStyle(
color: colorScheme.onSurface,
fontSize: defaultFontSize,
),
);
TextStyle regularTextStyleWithColor(Color? color, {Color? backgroundColor}) =>
regularTextStyle.copyWith(color: color, backgroundColor: backgroundColor);
TextStyle get _smallText =>
regularTextStyle.copyWith(fontSize: smallFontSize);
TextStyle get _largeText =>
regularTextStyle.copyWith(fontSize: largeFontSize);
TextStyle get errorTextStyle => regularTextStyleWithColor(colorScheme.error);
TextStyle get boldTextStyle =>
regularTextStyle.copyWith(fontWeight: FontWeight.bold);
TextStyle get subtleTextStyle =>
regularTextStyle.copyWith(color: colorScheme.subtleTextColor);
TextStyle get fixedFontStyle => fixBlurryText(
regularTextStyle.copyWith(
fontFamily: 'RobotoMono',
// Slightly smaller for fixes font text since it will appear larger
// to begin with.
fontSize: defaultFontSize - 1,
),
);
TextStyle get subtleFixedFontStyle => fixedFontStyle.copyWith(
color: colorScheme.subtleTextColor,
);
TextStyle get selectedSubtleTextStyle =>
subtleTextStyle.copyWith(color: colorScheme.onSurface);
TextStyle get tooltipFixedFontStyle => fixedFontStyle.copyWith(
color: colorScheme.tooltipTextColor,
);
TextStyle get fixedFontLinkStyle => fixedFontStyle.copyWith(
color: colorScheme._devtoolsLink,
decoration: TextDecoration.underline,
);
TextStyle get linkTextStyle => fixBlurryText(
TextStyle(
color: colorScheme._devtoolsLink,
decoration: TextDecoration.underline,
fontSize: defaultFontSize,
),
);
TextStyle get subtleChartTextStyle => fixBlurryText(
TextStyle(
color: colorScheme._chartSubtleColor,
fontSize: smallFontSize,
),
);
TextStyle get searchMatchHighlightStyle => fixBlurryText(
const TextStyle(
color: Colors.black,
backgroundColor: activeSearchMatchColor,
),
);
TextStyle get searchMatchHighlightStyleFocused => fixBlurryText(
const TextStyle(
color: Colors.black,
backgroundColor: searchMatchColor,
),
);
TextStyle get legendTextStyle => fixBlurryText(
TextStyle(
fontWeight: FontWeight.normal,
fontSize: smallFontSize,
decoration: TextDecoration.none,
),
);
}
/// Returns a [TextStyle] with [FontFeature.proportionalFigures] applied to
/// fix blurry text.
TextStyle fixBlurryText(TextStyle style) {
return style.copyWith(
fontFeatures: [const FontFeature.proportionalFigures()],
);
}
// Duration and animation constants:
/// A short duration to use for animations.
///
/// Use this when you want less emphasis on the animation and more on the
/// animation result, or when you have multiple animations running in sequence
/// For example, in the timeline we use this when we are zooming the flame chart
/// and scrolling to an offset immediately after.
const shortDuration = Duration(milliseconds: 50);
/// A longer duration than [shortDuration] but quicker than [defaultDuration].
///
/// Use this for thinks that would show a bit of animation, but that we want to
/// effectively seem immediate to users.
const rapidDuration = Duration(milliseconds: 100);
/// The default duration to use for animations.
const defaultDuration = Duration(milliseconds: 200);
/// A long duration to use for animations.
///
/// Use this rarely, only when you want added emphasis to an animation.
const longDuration = Duration(milliseconds: 400);
/// Builds a [defaultDuration] animation controller.
///
/// This is the standard duration to use for animations.
AnimationController defaultAnimationController(
TickerProvider vsync, {
double? value,
}) {
return AnimationController(
duration: defaultDuration,
vsync: vsync,
value: value,
);
}
/// Builds a [longDuration] animation controller.
///
/// This is the standard duration to use for slow animations.
AnimationController longAnimationController(
TickerProvider vsync, {
double? value,
}) {
return AnimationController(
duration: longDuration,
vsync: vsync,
value: value,
);
}
/// The default curve we use for animations.
///
/// Inspector animations benefit from a symmetric animation curve which makes
/// it easier to reverse animations.
const defaultCurve = Curves.easeInOutCubic;
/// Builds a [CurvedAnimation] with [defaultCurve].
///
/// This is the standard curve for animations in DevTools.
CurvedAnimation defaultCurvedAnimation(AnimationController parent) =>
CurvedAnimation(curve: defaultCurve, parent: parent);
/// Measures the screen size to determine whether it is strictly larger
/// than [width], scaled to the current font factor.
bool isScreenWiderThan(
BuildContext context,
double? width,
) {
return width == null ||
MediaQuery.of(context).size.width > scaleByFontFactor(width);
}
ButtonStyle denseAwareOutlinedButtonStyle(
BuildContext context,
double? minScreenWidthForTextBeforeScaling,
) {
final buttonStyle =
Theme.of(context).outlinedButtonTheme.style ?? const ButtonStyle();
return _generateButtonStyle(
context: context,
buttonStyle: buttonStyle,
minScreenWidthForTextBeforeScaling: minScreenWidthForTextBeforeScaling,
);
}
ButtonStyle denseAwareTextButtonStyle(
BuildContext context, {
double? minScreenWidthForTextBeforeScaling,
}) {
final buttonStyle =
Theme.of(context).textButtonTheme.style ?? const ButtonStyle();
return _generateButtonStyle(
context: context,
buttonStyle: buttonStyle,
minScreenWidthForTextBeforeScaling: minScreenWidthForTextBeforeScaling,
);
}
ButtonStyle _generateButtonStyle({
required BuildContext context,
required ButtonStyle buttonStyle,
double? minScreenWidthForTextBeforeScaling,
}) {
if (!isScreenWiderThan(context, minScreenWidthForTextBeforeScaling)) {
buttonStyle = buttonStyle.copyWith(
padding: WidgetStateProperty.resolveWith<EdgeInsets>((_) {
return EdgeInsets.zero;
}),
);
}
return buttonStyle;
}