backgroundImageSize property

Size? get backgroundImageSize

Implementation

Size? get backgroundImageSize {
  // Prefer the per-layer destination size when provided by the painter.
  if (_currentLayerDestSizeForPercent != null) {
    return _currentLayerDestSizeForPercent;
  }
  final ui.Image? image = _imagePainter?._image?.image;
  if (image == null) return null;

  final double imageWidth = image.width.toDouble();
  final double imageHeight = image.height.toDouble();
  final double aspectRatio = imageWidth / imageHeight;

  final CSSBackgroundSize bs = renderStyle.backgroundSize;
  final CSSLengthValue? backgroundWidth = bs.width;
  final CSSLengthValue? backgroundHeight = bs.height;

  // Only width is set (e.g., `100px` or `100px auto`).
  if (backgroundWidth != null &&
      !backgroundWidth.isAuto &&
      backgroundWidth.computedValue > 0 &&
      (backgroundHeight == null || backgroundHeight.isAuto)) {
    final double w = backgroundWidth.computedValue;
    final double h = w / aspectRatio;
    return Size(w, h);
  }

  // Only height is set (e.g., `auto 100px`).
  if (backgroundHeight != null &&
      !backgroundHeight.isAuto &&
      backgroundHeight.computedValue > 0 &&
      (backgroundWidth == null || backgroundWidth.isAuto)) {
    final double h = backgroundHeight.computedValue;
    final double w = h * aspectRatio;
    return Size(w, h);
  }

  // Both width and height are set (e.g., `100px 100px`).
  if (backgroundWidth != null &&
      !backgroundWidth.isAuto &&
      backgroundWidth.computedValue > 0 &&
      backgroundHeight != null &&
      !backgroundHeight.isAuto &&
      backgroundHeight.computedValue > 0) {
    return Size(backgroundWidth.computedValue, backgroundHeight.computedValue);
  }

  // For keyword values (auto/contain/cover) we cannot compute without the
  // painting rect; return intrinsic size as a fallback.
  return Size(imageWidth, imageHeight);
}