clampWeights static method
Clamps small slots to smallMin–smallMax and large slots to largeMax.
The leftover width is shared by the other slots so the row still fills
viewport.
Implementation
static List<int> clampWeights({
required List<int> pattern,
required double viewport,
required double smallMin,
required double smallMax,
required double largeMax,
}) {
if (pattern.isEmpty || viewport <= 0) {
return pattern;
}
final int sum = pattern.fold<int>(0, (int a, int b) => a + b);
if (sum <= 0) {
return pattern;
}
final int minWeight = pattern.reduce(math.min);
final int maxWeight = pattern.reduce(math.max);
final double unit = viewport / sum;
final slots = <double>[for (final int weight in pattern) weight * unit];
final small = <bool>[for (final int weight in pattern) weight == minWeight];
final large = <bool>[
for (final int weight in pattern)
weight == maxWeight && weight != minWeight,
];
_clampExtremeSlots(
slots: slots,
small: small,
large: large,
smallMin: smallMin,
smallMax: smallMax,
largeMax: largeMax,
);
final List<int> targets = _receiverIndices(small: small, large: large);
_shareRemainingWidth(
slots: slots,
targets: targets,
viewport: viewport,
large: large,
largeMax: largeMax,
);
return _roundSlots(slots: slots, targets: targets, viewport: viewport);
}