generateAppIcon function
Future<void>
generateAppIcon(
)
Implementation
Future<void> generateAppIcon() async {
DartgenxConfig config = DartgenxConfig.load();
String? imagePath;
if (config.inputPath?.appIcon == null) {
imagePath = Input(
prompt: 'Enter the full path of app icon image: ',
validator: (v) {
final file = File(v);
bool isPng = file.path.split('.').last == 'png';
if (file.existsSync() && isPng) {
return true;
}
throw ValidationError("Please enter a valid image path and the image should be in png format.");
},
).interact();
} else {
imagePath = config.inputPath?.appIcon;
}
final imageFile = File(imagePath!);
final listOfPlatform = [
androidTemplate,
iosTemplate,
macosTemplate,
windowsTemplate,
webTemplate,
];
final image = decodeImage(imageFile.readAsBytesSync())!;
for (var template in listOfPlatform) {
final Directory platformDirectory = Directory(template.directoryPath);
if (!platformDirectory.existsSync()) {
continue;
}
if (template.imageDirectory != null) {
for (var fileDirectory in template.imageDirectory!) {
final fileName = template.sizes[fileDirectory].keys.first;
final size = template.sizes[fileDirectory].values.first;
final resized = copyResize(image, width: size, height: size);
final outputFile = File(
path.join(template.directoryPath, fileDirectory, "$fileName.${template.extension}"),
);
await outputFile.writeAsBytes(encodePng(resized));
}
} else {
final files = platformDirectory.listSync(recursive: false);
final pngFiles = files.where((file) => file.path.toLowerCase().endsWith('.${template.extension}')).toList();
for (final png in pngFiles) {
if (png is File && png.path.toLowerCase().endsWith('.${template.extension}')) {
try {
png.deleteSync();
} catch (e) {
// Handles exception
}
}
}
for (var fileName in template.sizes.keys) {
final size = template.sizes[fileName];
final resized = copyResize(image, width: size, height: size);
final outputFile = File(
path.join(template.directoryPath, "$fileName.${template.extension}"),
);
await outputFile.writeAsBytes(encodePng(resized));
}
}
}
ConsoleLog.success("App icons generated successfully 😁");
}