getLargestAvailableSpace method

Rect getLargestAvailableSpace(
  1. ElementModel elementModel
)

This functions search for the maximum rect available space We use it for example to find the most available space to write a text in our anchored helper

Implementation

Rect getLargestAvailableSpace(ElementModel elementModel) {
  var parentObject = context.findRenderObject()!;
  var element = elementModel.element!;
  var translation =
      element.renderObject!.getTransformTo(parentObject).getTranslation();
  var objectX = translation.x;
  var objectEndX = objectX + element.size!.width;
  var objectY = translation.y;
  var objectEndY = objectY + element.size!.height;
  var layerRect = parentObject.paintBounds;

  Rect availableHSpace;
  Rect availableWSpace;
  if (objectY > layerRect.height - objectEndY) {
    availableHSpace = Rect.fromLTWH(0, 0, layerRect.width, objectY);
  } else {
    availableHSpace = Rect.fromLTWH(
        0, objectEndY, layerRect.width, layerRect.height - objectEndY);
  }
  if (objectX > layerRect.width - objectEndX) {
    availableWSpace = Rect.fromLTWH(0, 0, objectX, layerRect.height);
  } else {
    availableWSpace = Rect.fromLTWH(
        objectEndX, 0, layerRect.width - objectEndX, layerRect.height);
  }
  // check area to use the largest
  var availableHSpaceArea =
      availableHSpace.size.width * availableHSpace.size.height;
  var availableWSpaceArea =
      availableWSpace.size.width * availableWSpace.size.height;
  if (availableWSpaceArea > availableHSpaceArea &&
      availableWSpace.width > minWritableSpace) {
    return availableWSpace;
  }
  return availableHSpace;
}