expressiveMultiBrowseKeylines function

ExpressiveCarouselKeylines expressiveMultiBrowseKeylines({
  1. required double mainAxisSize,
  2. required double preferredItemSize,
  3. required double itemSpacing,
  4. required int itemCount,
  5. required double minSmallItemSize,
  6. required double maxSmallItemSize,
  7. required double anchorSize,
})

Compose's multiBrowseKeylineList: as many large items of preferredItemSize as fit, then one medium and one small item peeking. Every length in the same unit (device pixels in a carousel).

Implementation

ExpressiveCarouselKeylines expressiveMultiBrowseKeylines({
  required double mainAxisSize,
  required double preferredItemSize,
  required double itemSpacing,
  required int itemCount,
  required double minSmallItemSize,
  required double maxSmallItemSize,
  required double anchorSize,
}) {
  if (mainAxisSize == 0 || preferredItemSize == 0) {
    return ExpressiveCarouselKeylines.empty;
  }
  var smallCounts = <int>[1];
  const mediumCounts = <int>[1, 0];
  final targetLarge = math.min(preferredItemSize, mainAxisSize);
  // A small item is a third of a large one, within its bounds (Compose).
  final targetSmall = (targetLarge / 3).clamp(
    minSmallItemSize,
    maxSmallItemSize,
  );
  final targetMedium = (targetLarge + targetSmall) / 2;
  if (mainAxisSize < minSmallItemSize * 2) smallCounts = <int>[0];
  final minLargeSpace =
      mainAxisSize -
      targetMedium * mediumCounts.reduce(math.max) -
      maxSmallItemSize * smallCounts.reduce(math.max);
  final minLargeCount = math.max(1, (minLargeSpace / targetLarge).floor());
  final maxLargeCount = (mainAxisSize / targetLarge).ceil();
  final largeCounts = _descending(maxLargeCount, minLargeCount);
  var arrangement = _Arrangement.findLowestCost(
    availableSpace: mainAxisSize,
    itemSpacing: itemSpacing,
    targetSmallSize: targetSmall,
    minSmallSize: minSmallItemSize,
    maxSmallSize: maxSmallItemSize,
    smallCounts: smallCounts,
    targetMediumSize: targetMedium,
    mediumCounts: mediumCounts,
    targetLargeSize: targetLarge,
    largeCounts: largeCounts,
  );
  if (arrangement != null && arrangement.itemCount > itemCount) {
    var surplus = arrangement.itemCount - itemCount;
    var smallCount = arrangement.smallCount;
    var mediumCount = arrangement.mediumCount;
    while (surplus > 0) {
      if (smallCount > 0) {
        smallCount -= 1;
      } else if (mediumCount > 1) {
        mediumCount -= 1;
      }
      surplus -= 1;
    }
    arrangement = _Arrangement.findLowestCost(
      availableSpace: mainAxisSize,
      itemSpacing: itemSpacing,
      targetSmallSize: targetSmall,
      minSmallSize: minSmallItemSize,
      maxSmallSize: maxSmallItemSize,
      smallCounts: <int>[smallCount],
      targetMediumSize: targetMedium,
      mediumCounts: <int>[mediumCount],
      targetLargeSize: targetLarge,
      largeCounts: largeCounts,
    );
  }
  if (arrangement == null) return ExpressiveCarouselKeylines.empty;
  return _leftAligned(
    mainAxisSize,
    itemSpacing,
    anchorSize,
    anchorSize,
    arrangement,
  );
}