themeProblems function
What of theme (a theme's JSON: a site manifest's "theme") the theme
does not read, each with the path of its key and what is accepted there:
a key a theme does not have, a color that is neither a value
("#AARRGGBB", "#RRGGBB") nor a name the theme's colors define
("surface": "cream" with no "cream" in colors), a family that is
not a text, a radius that is not a number, a mode that is not one.
colors: the color names the theme is laid over (a design system's), each
a name a color may give too. Empty for a theme that is null.
Implementation
List<ThemeProblem> themeProblems(Object? theme, {Iterable<String> colors = const []}) {
final out = <ThemeProblem>[];
if (theme == null) return out;
final top = readMap(theme);
if (top == null) {
return [(path: const [], message: 'not read: it is an object, {"primary": "#FF2563EB", "surface": "#FFFFFFFF", "colors": {"ink": "#FF111827"}}.')];
}
String show(Object? v) {
final s = v is String ? '"$v"' : '$v';
return s.length > 40 ? '${s.substring(0, 40)}…' : s;
}
void read(Map<String, Object?> m, List<String> at, Set<String> outer) {
final own = readMap(m['colors']) ?? const <String, Object?>{};
final names = {...outer, ...own.keys, for (final r in ThemeTokens.roleNames) if (m[r] != null || outer.contains(r)) r};
void color(List<String> path, Object? v) {
if (v == null || Color.fromHex(v) != null) return;
final defined = [...own.keys, ...{...outer}..removeAll(own.keys)..removeAll(ThemeTokens.roleNames)];
final has = defined.isEmpty ? 'this theme\'s "colors" define none' : 'here: ${defined.take(12).join(', ')}${defined.length > 12 ? '…' : ''}';
if (v is String && Color.isName(v)) {
// A name: one the colors define, or a role that is given.
if (names.contains(v) && v != path.last) return;
out.add((
path: path,
message: 'no color of the theme is named ${show(v)}, so it is not read. '
'Write its value, "#AARRGGBB" (or "#RRGGBB"), or define it: "colors": {"$v": "#AARRGGBB"} ($has).',
));
return;
}
out.add((
path: path,
message: '${show(v)} is not a color, so it is not read. '
'Write "#AARRGGBB" (or "#RRGGBB"), or the name of a color the theme\'s "colors" define ($has).',
));
}
for (final e in m.entries) {
final key = e.key, v = e.value, path = [...at, key];
if (v == null) continue;
if (!themeKeys.contains(key) || (key == 'dark' && at.isNotEmpty)) {
final to = _themeKeyFor[key];
final what = switch (to) {
'surface' => ' (the ground a page is drawn on)',
'primary' => ' (the accent)',
'onSurface' => ' (the color of the text)',
'titleFontFamily' => ' (the family of titles and headings)',
_ => '',
};
final instead = to != null
? ' Write "$to"$what.'
: Color.fromJson(v) != null
? ' A color of the site\'s own goes in "colors": {"$key": ${show(v)}}.'
: '';
out.add((
path: path,
message: 'a theme has no "$key", so it is not read.$instead '
'A theme\'s keys: ${themeKeys.where((k) => k != 'media' && (at.isEmpty || k != 'dark')).join(', ')}.',
));
continue;
}
switch (key) {
case 'primary' || 'onPrimary' || 'surface' || 'onSurface' || 'error' || 'success' || 'warning':
color(path, v);
case 'fontFamily' || 'titleFontFamily' || 'iconStyle':
if (v is! String || v.trim().isEmpty) {
out.add((path: path, message: '${show(v)} is not read: write ${key == 'iconStyle' ? 'outlined, rounded or sharp' : 'a font family\'s name'} as a text ("${key == 'iconStyle' ? 'rounded' : 'Georgia'}").'));
}
case 'radius' || 'buttonRadius':
if (v is! num) out.add((path: path, message: '${show(v)} is not read: write a number (8).'));
case 'mode':
if (ThemeMode.tryParse(v) == null) out.add((path: path, message: '${show(v)} is not a mode: write "system", "light" or "dark".'));
case 'colors':
if (readMap(v) == null) {
out.add((path: path, message: 'not read: it is an object of names and colors, {"ink": "#FF111827"}.'));
} else {
for (final c in own.entries) {
color([...path, c.key], c.value);
}
}
case 'textStyles':
final styles = readMap(v);
if (styles == null) {
out.add((path: path, message: 'not read: it is an object of names and text styles, {"heading1": {"fontFamily": "Georgia", "fontSize": 40, "fontWeight": 700}}.'));
} else {
for (final s in styles.entries) {
final style = readMap(s.value);
if (style == null) {
out.add((path: [...path, s.key], message: '${show(s.value)} is not a text style, so it is not read: write {"fontFamily": "Georgia", "fontSize": 40, "fontWeight": 700, "color": "#FF111827"}.'));
continue;
}
for (final k in const ['color', 'decorationColor', 'backgroundColor']) {
color([...path, s.key, k], style[k]);
}
}
}
case 'spacing' || 'radii' || 'sizes':
final scale = readMap(v);
if (scale == null) {
out.add((path: path, message: 'not read: it is an object of names and numbers, {"card": 12}.'));
} else {
for (final n in scale.entries) {
if (n.value is! num) out.add((path: [...path, n.key], message: '${show(n.value)} is not read: write a number.'));
}
}
case 'dark':
final d = readMap(v);
if (d == null) {
out.add((path: path, message: 'not read: it is an object of the colors that differ in dark mode, {"surface": "#FF111111", "colors": {"ink": "#FFF3F4F6"}}.'));
} else {
read(d, path, names);
}
}
}
}
// A design system has every role.
read(top, const [], {...colors, if (colors.isNotEmpty) ...ThemeTokens.roleNames});
return out;
}