createImageList function

List<Map<String, dynamic>> createImageList(
  1. String fileNamePrefix,
  2. String? darkFileNamePrefix,
  3. String? tintedFileNamePrefix
)

Create the image list for the Contents.json file for Xcode versions Xcode 14 and above

Implementation

List<Map<String, dynamic>> createImageList(String fileNamePrefix, String? darkFileNamePrefix, String? tintedFileNamePrefix) {
  const List<Map<String, dynamic>> imageConfigurations = [
    {'size': '20x20', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x', '3x']},
    {'size': '29x29', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x', '3x']},
    {'size': '38x38', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x', '3x']},
    {'size': '40x40', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x', '3x']},
    {'size': '60x60', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x', '3x']},
    {'size': '64x64', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x', '3x']},
    {'size': '68x68', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x']},
    {'size': '76x76', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x']},
    {'size': '83.5x83.5', 'idiom': 'universal', 'platform': 'ios', 'scales': ['2x']},
    {'size': '1024x1024', 'idiom': 'universal', 'platform': 'ios', 'scales': ['1x']},
    {'size': '1024x1024', 'idiom': 'ios-marketing', 'scales': ['1x']},
  ];

  final List<Map<String, dynamic>> imageList = <Map<String, dynamic>>[];

  for (final config in imageConfigurations) {
    final size = config['size']!;
    final idiom = config['idiom']!;
    final platform = config['platform'];
    final List<String> scales = config['scales'];

    for (final scale in scales) {
      final filename = '$fileNamePrefix-$size@$scale.png';
      imageList.add(
        ContentsImageObject(
          size: size,
          idiom: idiom,
          filename: filename,
          platform: platform,
          scale: scale,
        ).toJson(),
      );
    }
  }

  // Prevent ios-marketing icon from being tinted or dark

  if (darkFileNamePrefix != null) {
    for (final config in imageConfigurations.where((e) => e['idiom'] == 'universal')) {
      final size = config['size']!;
      final idiom = config['idiom']!;
      final platform = config['platform'];
      final List<String> scales = config['scales'];

      for (final scale in scales) {
        final filename = '$darkFileNamePrefix-$size@$scale.png';
        imageList.add(
          ContentsImageObject(
            size: size,
            idiom: idiom,
            filename: filename,
            platform: platform,
            scale: scale,
            appearances: <ContentsImageAppearanceObject>[
              ContentsImageAppearanceObject(appearance: 'luminosity', value: 'dark'),
            ],
          ).toJson(),
        );
      }
    }
  }

  if (tintedFileNamePrefix != null) {
    for (final config in imageConfigurations.where((e) => e['idiom'] == 'universal')) {
      final size = config['size']!;
      final idiom = config['idiom']!;
      final platform = config['platform'];
      final List<String> scales = config['scales'];

      for (final scale in scales) {
        final filename = '$tintedFileNamePrefix-$size@$scale.png';
        imageList.add(
          ContentsImageObject(
            size: size,
            idiom: idiom,
            filename: filename,
            platform: platform,
            scale: scale,
            appearances: <ContentsImageAppearanceObject>[
              ContentsImageAppearanceObject(appearance: 'luminosity', value: 'tinted'),
            ],
          ).toJson(),
        );
      }
    }
  }

  return imageList;
}