computeSpoilerTextLayout function

(SpoilerGeometry, TextPainter) computeSpoilerTextLayout({
  1. required String text,
  2. required TextStyle? style,
  3. required TextAlign textAlign,
  4. required TextDirection textDirection,
  5. int? maxLines,
  6. TextScaler textScaler = TextScaler.noScaling,
  7. TextHeightBehavior? textHeightBehavior,
  8. Locale? locale,
  9. StrutStyle? strutStyle,
  10. bool isEllipsis = false,
  11. TextRange? range,
  12. required double maxWidth,
})

Compute layout and word regions for spoiler text.

If range == null:

  • walk the whole text and collect non-whitespace "words". If range != null:
  • collect selection boxes only for that range.

Implementation

(SpoilerGeometry geometry, TextPainter painter) computeSpoilerTextLayout({
  required String text,
  required TextStyle? style,
  required TextAlign textAlign,
  required TextDirection textDirection,
  int? maxLines,
  TextScaler textScaler = TextScaler.noScaling,
  TextHeightBehavior? textHeightBehavior,
  Locale? locale,
  StrutStyle? strutStyle,
  bool isEllipsis = false,
  TextRange? range,
  required double maxWidth,
}) {
  final painter = TextPainter(
    text: TextSpan(text: text, style: style),
    textDirection: textDirection,
    textAlign: textAlign,
    maxLines: maxLines,
    ellipsis: isEllipsis ? '…' : null,
    textScaler: textScaler,
    textHeightBehavior: textHeightBehavior,
    locale: locale,
    strutStyle: strutStyle,
  );

  painter.layout(maxWidth: maxWidth);

  final selection = range == null
      ? TextSelection(baseOffset: 0, extentOffset: text.length)
      : TextSelection(
          baseOffset: range.start.clamp(0, text.length),
          extentOffset: range.end.clamp(0, text.length),
        );

  final geom = buildSpoilerGeometry(
    layout: TextPainterLayoutClient(painter),
    text: text,
    selection: selection,
    skipWhitespace: true,
  );

  return (geom!, painter);
}