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.
| API | Returns | Typical use |
|---|---|---|
fastCoverScanSize | Size | Layout (SizedBox, custom paint) |
fastCoverScanScale | double | Transform.scale, animation, multiply original dimensions |
Clip overflow with ClipRect / ClipRRect; common cover patterns are in Cover layout.
Algorithm
- Zero dimensions — If any side of
parentSizeorchildSizeis0,fastCoverScanSizereturnschildSizeunchanged;fastCoverScanScalereturns1.0(avoids division by zero). - Aspect ratio —
aspect = width / heightfor parent and child. - 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
- Parent aspect ≥ child aspect: align by width,
- Relationship — With non-zero sizes,
fastCoverScanSize(parent, child)equalsSize(child.width * s, child.height * s)wheres = fastCoverScanScale(parent, child).smay 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
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)// Parent 200×100, child 100×100 — child is “squarer”, align by height
fastCoverScanSize(Size(200, 100), Size(100, 100));
// Size(200.0, 200.0)fastCoverScanSize(Size(100, 0), Size(50, 80));
// Size(50.0, 80.0) — original childSizeAPI reference
fastCoverScanSize
Size fastCoverScanSize(Size parentSize, Size childSize);| Parameter | Type | Required | Description |
|---|---|---|---|
parentSize | Size | yes | Parent (viewport) size. |
childSize | Size | yes | Child’s original size. |
| Return | Type | Description |
|---|---|---|
| Cover size | Size | Same 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
fastCoverScanScale(Size(100, 100), Size(50, 50));
// 2.0 — same aspect ratio, by width: 100 / 50// Parent 100×100, child 200×100 — height alignment is enough
fastCoverScanScale(Size(100, 100), Size(200, 100));
// 1.0// Parent 100×100, child 100×50
fastCoverScanScale(Size(100, 100), Size(100, 50));
// 2.0 — parent.height / child.heightAPI reference
fastCoverScanScale
double fastCoverScanScale(Size parentSize, Size childSize);| Parameter | Type | Required | Description |
|---|---|---|---|
parentSize | Size | yes | Parent size. |
childSize | Size | yes | Child’s original size. |
| Return | Type | Description |
|---|---|---|
| Scale factor | double | 1.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:
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.