createAdaptiveIcons function

Future<void> createAdaptiveIcons(
  1. Config config,
  2. String? flavor, {
  3. required FLILogger logger,
  4. String prefixPath = '.',
})

Implementation

Future<void> createAdaptiveIcons(
  Config config,
  String? flavor, {
  required FLILogger logger,
  String prefixPath = '.',
}) async {
  logger.info('Creating adaptive icons Android');

  // Retrieve the necessary Flutter Launcher Icons configuration from the pubspec.yaml file
  final String? backgroundConfig = config.adaptiveIconBackground;
  final String? foregroundImagePath = config.adaptiveIconForeground;
  if (backgroundConfig == null || foregroundImagePath == null) {
    throw const InvalidConfigException(constants.errorMissingImagePath);
  }
  rejectVectorDrawableSource(
    field: 'adaptive_icon_foreground',
    value: foregroundImagePath,
  );
  var foregroundImage = await utils.decodeImageFile(
    path.join(prefixPath, foregroundImagePath),
  );

  // Letter-box a non-square foreground so every mipmap size keeps the
  // source's aspect ratio (upstream #214). Color resolution: adaptive
  // background if it's a hex literal, else the top-level `background_color`
  // fallback. When neither is a color (e.g. PNG-only adaptive background
  // and no top-level default) we keep the legacy squish behavior.
  final adaptiveLetterBoxColor = !isAdaptiveIconConfigPngFile(backgroundConfig)
      ? utils.parseHexColor(backgroundConfig)
      : (config.backgroundColor != null
            ? utils.parseHexColor(config.backgroundColor!)
            : null);
  if (adaptiveLetterBoxColor != null) {
    foregroundImage = utils.letterBoxToSquare(
      foregroundImage,
      adaptiveLetterBoxColor,
    );
  }

  final concurrentImageUpdates = <Future<void>>[];
  // Create adaptive icon foreground images
  for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) {
    concurrentImageUpdates.add(
      overwriteExistingIcons(
        androidIcon,
        foregroundImage,
        constants.androidAdaptiveForegroundFileName,
        flavor,
        prefixPath: prefixPath,
      ),
    );
  }

  // Create adaptive icon background
  if (isAdaptiveIconConfigPngFile(backgroundConfig)) {
    concurrentImageUpdates.add(
      _createAdaptiveBackgrounds(
        config,
        backgroundConfig,
        flavor,
        prefixPath: prefixPath,
      ),
    );
  } else {
    await updateColorsXmlFile(
      backgroundConfig,
      flavor,
      logger: logger,
      prefixPath: prefixPath,
    );
  }
  await Future.wait(concurrentImageUpdates);
}