row static method

void row(
  1. List<GDisplayObject> items, {
  2. double gap = 0,
  3. double startX = 0,
  4. double startY = 0,
  5. double width = 0,
  6. double height = 0,
  7. MainAxisAlignment axisAlign = MainAxisAlignment.start,
  8. CrossAxisAlignment crossAlign = CrossAxisAlignment.start,
  9. bool mask = false,
  10. bool debug = false,
})

Arranges the items horizontally in a single row, similar to Flutter's Row, with optional gap between them. The row will start at the startX and startY position. The width and height of the row can be specified, but at least one of them must be greater than 0. The axisAlign parameter controls how the items are aligned along the horizontal axis, while the crossAlign parameter controls how they are aligned along the vertical axis. If mask is set to true, the parent container of the items will be clipped to the specified width and height. If debug is set to true, a red rectangle will be drawn around the row for debugging purposes. Make sure all items belongs to the same parent.

Implementation

static void row(
  List<GDisplayObject> items, {
  double gap = 0,
  double startX = 0,
  double startY = 0,
  double width = 0,
  double height = 0,
  MainAxisAlignment axisAlign = MainAxisAlignment.start,
  CrossAxisAlignment crossAlign = CrossAxisAlignment.start,
  bool mask = false,
  bool debug = false,
}) {
  var currentX = .0, maxH = .0, maxW = .0, itemsW = .0;
  final numItems = items.length;
  if (numItems == 0) {
    return;
  }

  /// default to start.
  for (var i = 0; i < numItems; ++i) {
    var itm = items[i];
    itm.y = startY;
    itm.x = startX + currentX;
    maxH = Math.max(maxH, itm.height);
    var itmW = itm.width;
    if (itm is GText && itmW.isInfinite) {
      itmW = itm.textWidth;
    }
    itemsW += itmW;
    currentX += itmW + gap;
  }

  if (height <= 0) {
    height = maxH;
  }

  final parent = items.first.parent as GSprite?;
  final hasSize = width > 0 && height > 0;
  if (debug && parent != null && hasSize) {
    final g = parent.graphics;
    g.beginFill(debugColor).drawRect(startX, startY, width, height).endFill();
  }

  maxW = currentX - gap;
  currentX = 0;
  if (mask && parent != null && hasSize) {
    parent.maskRect = GRect(startX, startY, width, height);
  }
  if (crossAlign == CrossAxisAlignment.center) {
    for (var i = 0; i < numItems; ++i) {
      var itm = items[i];
      itm.y = startY + (height - itm.height) / 2;
    }
  } else if (crossAlign == CrossAxisAlignment.end) {
    for (var i = 0; i < numItems; ++i) {
      var itm = items[i];
      itm.y = startY + height - itm.height;
    }
  }

  if (axisAlign == MainAxisAlignment.center) {
    var centerX = (width - maxW) / 2;
    startX += centerX;
    for (var i = 0; i < numItems; ++i) {
      var itm = items[i];
      itm.x = startX + currentX;
      var itmW = itm.width;
      if (itm is GText && itmW.isInfinite) {
        itmW = itm.textWidth;
      }
      currentX += itmW + gap;
    }
  } else if (axisAlign == MainAxisAlignment.end) {
    startX += width - maxW;
    for (var i = 0; i < numItems; ++i) {
      var itm = items[i];
      itm.x = startX + currentX;
      var itmW = itm.width;
      if (itm is GText && itmW.isInfinite) {
        itmW = itm.textWidth;
      }
      currentX += itmW + gap;
    }
  } else if (axisAlign == MainAxisAlignment.spaceEvenly) {
    /// calculate gap.
    gap = (width - itemsW) / (numItems + 1);
    startX += gap;
    for (var i = 0; i < numItems; ++i) {
      var itm = items[i];
      itm.x = startX + currentX;
      var itmW = itm.width;
      if (itm is GText && itmW.isInfinite) {
        itmW = itm.textWidth;
      }
      currentX += itmW + gap;
    }
  } else if (axisAlign == MainAxisAlignment.spaceBetween) {
    gap = (width - itemsW) / (numItems - 1);
    for (var i = 0; i < numItems; ++i) {
      var itm = items[i];
      itm.x = startX + currentX;
      var itmW = itm.width;
      if (itm is GText && itmW.isInfinite) {
        itmW = itm.textWidth;
      }
      currentX += itmW + gap;
    }
  } else if (axisAlign == MainAxisAlignment.spaceAround) {
    gap = (width - itemsW) / (numItems);
    startX += gap / 2;
    for (var i = 0; i < numItems; ++i) {
      var itm = items[i];
      itm.x = startX + currentX;
      var itmW = itm.width;
      if (itm is GText && itmW.isInfinite) {
        itmW = itm.textWidth;
      }
      currentX += itmW + gap;
    }
  }
}