setDimensions function

HTMLImageElement setDimensions(
  1. HTMLImageElement imageElement,
  2. String? dimensions
)

Implementation

html.HTMLImageElement setDimensions(html.HTMLImageElement imageElement, String? dimensions) {
  if (dimensions == null || dimensions.isEmpty) {
    console.error("null or empty dimenstions String, could not set dimenstions");
    return imageElement;
  }

  List<String> dimensionsSplit = dimensions.split(',');

  if (dimensionsSplit.length != 2) {
    console.error("Error: could not split dimensions into 2 Strings");
    return imageElement;
  }

  int? width = int.tryParse(dimensionsSplit[0]);
  int? height = int.tryParse(dimensionsSplit[1]);

  if (width == null || height == null) {
    console.error("Error: could not parse dimensions from String $dimensions");
    return imageElement;
  }

  console.verbose("width is $width and height is $height");

  imageElement.width = width;
  imageElement.height = height;

  return imageElement;
}