drawItems method

bool drawItems(
  1. Canvas canvas,
  2. Size size,
  3. String header,
  4. double offset,
)

Implementation

bool drawItems(Canvas canvas, Size size, String header, double offset) {
  List<GridImage>? items = map[header];

  double lastItemPosition = -scrollController.offset +
      getItemTop(items!.length - 1) +
      offset +
      boxSize;
  bool isInvisible = lastItemPosition < 0;
  if (isInvisible) return true;

  for (int i = 0; i < items.length; i++) {
    GridImage item = items[i];


    double position = -scrollController.offset + getItemTop(i) + offset;
    bool isInvisible = position < -boxSize ||
        position > scrollController.position.viewportDimension;
    if (isInvisible) continue;
    double itemSize = boxSize * (1 - padding);
    double posLeft = getItemLeft(i) + (boxSize - itemSize) * 0.5;
    double posTop = offset + getItemTop(i) + (boxSize - itemSize) * 0.5;

    double nWidth = 0;
    double nHeight = 0;
    double pT = posTop;
    double pL = posLeft;
    if(item.width >= item.height){
      nWidth = itemSize;
      nHeight = (item.height / item.width) * itemSize;
      double t1 = boxSize - nHeight;
      double t2 = t1 - nHeight;
      double t3 = t2 / 2;
      pT -= t3;
    }
    else{
      nHeight = itemSize;
      nWidth = (item.width / item.height) * itemSize;
      double t1 = boxSize - nWidth;
      double t2 = t1 - nWidth;
      double t3 = t2 / 2;
      pL -= t3;
    }
    canvas.drawImageRect(
        item.uiImage,
        // Source image bounds
        Rect.fromLTWH(item.x, item.y,
            item.width, item.height),
        // Destination image bounds
        Rect.fromLTWH(pL, pT, nWidth, nHeight),
        painter);
    if(clickedItemId == item.id){
      painter.color = const Color(0xff000000).withOpacity(blinkVal);
      canvas.drawRect(Rect.fromLTWH(posLeft - boxSize / 10, posTop - boxSize / 10, boxSize, boxSize), painter);
      painter.color = backgroundColor;
    }
  }
  return false;
}