getDragGridSize function

Size getDragGridSize({
  1. required int total,
  2. required Size viewSize,
  3. required double? mainAxisExtent,
  4. required double childAspectRatio,
  5. required double crossAxisSpacing,
  6. required double mainAxisSpacing,
  7. required int crossCount,
  8. required Axis direction,
})

Computing Drag Grid Size

Implementation

Size getDragGridSize({
  required int total,
  required Size viewSize,
  required double? mainAxisExtent,
  required double childAspectRatio,
  required double crossAxisSpacing,
  required double mainAxisSpacing,
  required int crossCount,
  required Axis direction,
}) {
  int line = 1;
  double width = 0.0;
  double height = 0.0;
  double ctxWidth = viewSize.width;
  double ctxHeight = viewSize.height;

  line = (total ~/ crossCount);
  line += (total % crossCount > 0 ? 1 : 0);

  if (direction == Axis.vertical) {
    width = (ctxWidth - crossAxisSpacing * (crossCount - 1)) / crossCount;

    if (mainAxisExtent == null) {
      height = width / childAspectRatio * line + mainAxisSpacing * (line - 1);
    }

    if (mainAxisExtent != null) {
      height = mainAxisExtent * line + mainAxisSpacing * (line - 1);
    }

    return Size(
      max(ctxWidth, 0),
      max(height, 0),
    );
  }

  if (direction == Axis.horizontal) {
    height = (ctxHeight - crossAxisSpacing * (crossCount - 1)) / crossCount;

    if (mainAxisExtent == null) {
      width = height / childAspectRatio * line + mainAxisSpacing * (line - 1);
    }

    if (mainAxisExtent != null) {
      width = mainAxisExtent * line + mainAxisSpacing * (line - 1);
    }

    return Size(
      max(width, 0),
      max(ctxHeight, 0),
    );
  }

  return Size.zero;
}