withValues method

MaterialFile withValues(
  1. Map<String, Object?> values
)

This material with values (by key, from animatableKeys, shaped as a file saves them: color an ARGB number, tiling [x, y] — or one of a pair's numbers alone, tiling#0) laid on top: what an entity's overrides draw. A key it does not know, or a value of the wrong type, is left out.

Implementation

MaterialFile withValues(Map<String, Object?> values) {
  if (values.isEmpty) return this;
  final merged = _all();
  for (final MapEntry(:key, :value) in values.entries) {
    // One number of a pair on its own (as a timeline keys it):
    // `tiling#0`.
    final hash = key.indexOf('#');
    if (hash >= 0 && value is num) {
      final pair = key.substring(0, hash);
      final i = int.tryParse(key.substring(hash + 1));
      if ((pair == 'tiling' || pair == 'offset') && (i == 0 || i == 1)) {
        final now = [...(merged[pair] as List)];
        now[i!] = value.toDouble();
        merged[pair] = now;
      }
      continue;
    }
    if (!animatableKeys.contains(key) || value == null) continue;
    final fits = switch (key) {
      'color' || 'emission' => value is num,
      'tiling' || 'offset' =>
        value is List && value.length == 2 && value.every((v) => v is num),
      _ => value is num,
    };
    if (fits) merged[key] = value;
  }
  return MaterialFile._fromJson(
    merged,
  ).copyWith(parent: parent, overrides: overrides);
}