createColors function
Creates a colors.xml
file to define background color for the splash.
Implementation
Future<void> createColors({
String? color,
bool isDark = false,
}) async {
if (color == null) {
log('No color is provided. Skip setting up color in Android');
return;
}
final androidValuesFolder = isDark
? CmdStrings.androidDarkValuesDirectory
: CmdStrings.androidValuesDirectory;
final colorsFilePath = '$androidValuesFolder/${AndroidStrings.colorXml}';
final xmlFile = File(colorsFilePath);
if (await xmlFile.exists()) {
final xmlDocument = XmlDocument.parse(xmlFile.readAsStringSync());
final resourcesElement =
xmlDocument.findAllElements(AndroidStrings.resourcesElement).first;
/// Check if `splashBackGroundColor` attribute value is already available
for (final element in resourcesElement.childElements) {
if (element.getAttribute(AndroidStrings.nameAttr) ==
AndroidStrings.nameAttrValue) {
/// Remove the attribute
element.remove();
break;
}
}
/// Add color element as child to resources element
resourcesElement.children.addAll([
XmlElement(
XmlName(AndroidStrings.colorElement),
[
XmlAttribute(
XmlName(AndroidStrings.nameAttr),
AndroidStrings.nameAttrValue,
),
],
[
XmlText(color),
],
),
]);
/// Write the updated XML back to the file
final updatedXmlString = xmlDocument.toXmlString(pretty: true);
await xmlFile.writeAsString(updatedXmlString);
} else {
/// If `colors.xml` file is not there, then create one
final xml = await xmlFile.create();
final builder = XmlBuilder();
builder.processing(AndroidStrings.xml, AndroidStrings.xmlVersion);
/// Create resources element and its children element color with its attribute
builder.element(AndroidStrings.resourcesElement, nest: () {
builder.element(
AndroidStrings.colorElement,
attributes: {
AndroidStrings.nameAttr: AndroidStrings.nameAttrValue,
},
nest: color,
);
});
final document = builder.buildDocument();
await xml.writeAsString(document.toXmlString(pretty: true));
}
}