Skip to content

Source on GitHublib/src/utils/fast_scan/

Overview

Top-level helpers fastCoverScanSize and fastCoverScanScale implement cover sizing similar to CSS object-fit: cover: given a parent box and a child’s intrinsic size, they compute either the target size or a uniform scale factor while preserving the child’s aspect ratio. Both take Flutter Size (width × height) and share the same algorithm.

APIReturnsTypical use
fastCoverScanSizeSizeLayout (SizedBox, custom paint)
fastCoverScanScaledoubleTransform.scale, animation, multiply original dimensions

Clip overflow with ClipRect / ClipRRect; common cover patterns are in Cover layout.


Algorithm

  1. Zero dimensions — If any side of parentSize or childSize is 0, fastCoverScanSize returns childSize unchanged; fastCoverScanScale returns 1.0 (avoids division by zero).
  2. Aspect ratioaspect = width / height for parent and child.
  3. Pick an axis (cover without distortion):
    • Parent aspect child aspect: align by width, scale = parent.width / child.width
    • Parent aspect < child aspect: align by height, scale = parent.height / child.height
  4. Relationship — With non-zero sizes, fastCoverScanSize(parent, child) equals
    Size(child.width * s, child.height * s) where s = fastCoverScanScale(parent, child).
    s may be less than 1 when the child is already larger but still needs cover alignment.

fastCoverScanSize

Returns the smallest uniformly scaled Size that covers parentSize while keeping the child’s aspect ratio (one dimension matches the parent; the other may extend beyond and is usually clipped).

Examples

dart
import 'package:fast_package/fast_package.dart';

// Parent 100×100, child 50×80 — parent is “squarer”, align by width
fastCoverScanSize(Size(100, 100), Size(50, 80));
// Size(100.0, 160.0)
dart
// Parent 200×100, child 100×100 — child is “squarer”, align by height
fastCoverScanSize(Size(200, 100), Size(100, 100));
// Size(200.0, 200.0)
dart
fastCoverScanSize(Size(100, 0), Size(50, 80));
// Size(50.0, 80.0) — original childSize

API reference


fastCoverScanSize

dart
Size fastCoverScanSize(Size parentSize, Size childSize);
ParameterTypeRequiredDescription
parentSizeSizeyesParent (viewport) size.
childSizeSizeyesChild’s original size.
ReturnTypeDescription
Cover sizeSizeSame aspect ratio as childSize; if any side is zero, returns childSize.

fastCoverScanScale

Returns the uniform scale factor s from the same rules; multiplying child width and height by s matches fastCoverScanSize.

Examples

dart
fastCoverScanScale(Size(100, 100), Size(50, 50));
// 2.0 — same aspect ratio, by width: 100 / 50
dart
// Parent 100×100, child 200×100 — height alignment is enough
fastCoverScanScale(Size(100, 100), Size(200, 100));
// 1.0
dart
// Parent 100×100, child 100×50
fastCoverScanScale(Size(100, 100), Size(100, 50));
// 2.0 — parent.height / child.height

API reference


fastCoverScanScale

dart
double fastCoverScanScale(Size parentSize, Size childSize);
ParameterTypeRequiredDescription
parentSizeSizeyesParent size.
childSizeSizeyesChild’s original size.
ReturnTypeDescription
Scale factordouble1.0 if any side is zero; otherwise width- or height-based ratio (may be greater or less than 1).

Flutter notes

When you know container and image pixel sizes, compute scale first and clip overflow:

dart
import 'package:flutter/material.dart';
import 'package:fast_package/fast_package.dart';

Widget coverImage({
  required Size containerSize,
  required Size imageSize,
  required ImageProvider image,
}) {
  final scale = fastCoverScanScale(containerSize, imageSize);

  return SizedBox(
    width: containerSize.width,
    height: containerSize.height,
    child: ClipRect(
      child: Center(
        child: Transform.scale(
          scale: scale,
          child: Image(
            image: image,
            width: imageSize.width,
            height: imageSize.height,
          ),
        ),
      ),
    ),
  );
}

For simple network images, Image with BoxFit.cover (see Cover layout) is often enough. These functions shine when you need numeric sizes for custom layout, animation, or non-Image children (canvas, video frames).

Typical cases: full-bleed backgrounds, video cover fit, thumbnail grids, responsive banners with fixed height.

Released under the MIT License