squeezedText function

List<String> squeezedText(
  1. RenderObject root, {
  2. double width = 140,
  3. int lines = 4,
})

Paragraphs squeezed into a narrow column, lines lines or more in less than width pixels: the sign of a layout that should reflow, such as actions that ought to move below their message on a phone.

Implementation

List<String> squeezedText(
  RenderObject root, {
  double width = 140,
  int lines = 4,
}) {
  final squeezed = <String>[];
  void visit(RenderObject node) {
    if (node is RenderParagraph && node.hasSize && node.size.width < width) {
      final text = node.text.toPlainText();
      final boxes = node.getBoxesForSelection(
        TextSelection(baseOffset: 0, extentOffset: text.length),
      );
      final rows = {for (final box in boxes) box.top.round()};
      if (rows.length >= lines) {
        squeezed.add(
          '"${text.length > 32 ? '${text.substring(0, 31)}…' : text}" '
          'in ${node.size.width.round()} px over ${rows.length} lines',
        );
      }
    }
    node.visitChildren(visit);
  }

  visit(root);
  return squeezed;
}