layout static method

TextPainter layout(
  1. String text,
  2. TextStyle style, {
  3. double? maxWidth,
  4. int? maxLines,
})

Returns text in style laid out left to right, at most maxWidth wide (unbounded when null) and with at most maxLines lines (unlimited when null).

Calls with an equal text, style, width and line limit return the same instance. The returned painter is shared: do not change or dispose it, and do not keep it past the current frame, because it may be evicted.

Implementation

static TextPainter layout(
  String text,
  TextStyle style, {
  double? maxWidth,
  int? maxLines,
}) {
  final key = (text, style, maxWidth, maxLines);
  final cached = _cache.remove(key);
  if (cached != null) {
    _cache[key] = cached;
    return cached;
  }

  final painter = TextPainter(
    text: TextSpan(text: text, style: style),
    textDirection: TextDirection.ltr,
    maxLines: maxLines,
  )..layout(maxWidth: maxWidth ?? double.infinity);

  _cache[key] = painter;
  if (_cache.length > maxEntries) {
    _cache.remove(_cache.keys.first);
  }
  return painter;
}