createSplashImageDrawable function

Future<void> createSplashImageDrawable({
  1. String? imageSource,
  2. String? color,
  3. String? gravity,
  4. String? backgroundImageSource,
  5. String? backgroundImageGravity,
})

Creates a new splash_screen.xml file to define the splash screen setup.

Implementation

Future<void> createSplashImageDrawable({
  String? imageSource,
  String? color,
  String? gravity,
  String? backgroundImageSource,
  String? backgroundImageGravity,
}) async {
  const androidDrawableFolder = CmdStrings.androidDrawableDirectory;
  const splashImagePath =
      '$androidDrawableFolder/${AndroidStrings.splashScreenXml}';
  final file = File(splashImagePath);

  final xml = await file.create();
  final builder = XmlBuilder();
  builder.processing(AndroidStrings.xml, AndroidStrings.xmlVersion);

  /// Creating a layer-list element and its attributes
  builder.element(AndroidStrings.layerListElement, nest: () {
    builder.attribute(
      AndroidStrings.xmlnsAndroidAttr,
      AndroidStrings.xmlnsAndroidAttrValue,
    );

    /// Creates item element and attributes for color
    if (color != null) {
      builder.element(
        AndroidStrings.itemElement,
        nest: () {
          builder.attribute(
            AndroidStrings.androidDrawableAttr,
            AndroidStrings.androidDrawableAttrVal,
          );
        },
      );
    }

    /// Creates item element and attributes for background image
    if (backgroundImageSource != null) {
      builder.element(AndroidStrings.itemElement, nest: () {
        builder.element(
          AndroidStrings.bitmapAttrVal,
          nest: () {
            builder.attribute(
              AndroidStrings.androidGravityAttr,
              backgroundImageGravity ??
                  AndroidStrings.defaultAndroidGravityAttrVal,
            );
            builder.attribute(
              AndroidStrings.androidSrcAttr,
              AndroidStrings.androidBackgroundSrcAttrVal,
            );
            builder.attribute(
              AndroidStrings.androidTileModeAttr,
              AndroidStrings.androidTileModeAttrVal,
            );
          },
        );
      });
    }

    /// Creates item element and attributes for image
    if (imageSource != null) {
      builder.element(AndroidStrings.itemElement, nest: () {
        builder.element(AndroidStrings.bitmapAttrVal, nest: () {
          builder.attribute(
            AndroidStrings.androidGravityAttr,
            gravity ?? AndroidStrings.defaultAndroidGravityAttrVal,
          );
          builder.attribute(
            AndroidStrings.androidSrcAttr,
            AndroidStrings.androidSrcAttrVal,
          );
          builder.attribute(
            AndroidStrings.androidTileModeAttr,
            AndroidStrings.androidTileModeAttrVal,
          );
        });
      });
    }
  });

  final document = builder.buildDocument();
  await xml.writeAsString(document.toXmlString(pretty: true));
  log("Created splash_screen.xml.");
}