constrainContent function

String constrainContent(
  1. String content, {
  2. int? width,
  3. int? height,
})

Implementation

String constrainContent(String content, {int? width, int? height}) {
  if (width != null && width <= 0) return '';
  if (height != null && height <= 0) return '';

  var result = content;
  if (width != null) {
    result = Layout.truncateLines(result, width, ellipsis: '...');
  }
  if (height != null) {
    result = Layout.truncateHeight(result, height);
  }

  final size = Layout.getSize(result);
  final targetWidth = width ?? size.width;
  final targetHeight = height ?? size.height;

  if (targetWidth == size.width && targetHeight == size.height) {
    return result;
  }

  return Layout.place(
    width: targetWidth,
    height: targetHeight,
    horizontal: HorizontalAlign.left,
    vertical: VerticalAlign.top,
    content: result,
  );
}