comicWidestWord function

double comicWidestWord(
  1. String text,
  2. TextStyle style, {
  3. TextScaler textScaler = TextScaler.noScaling,
})

The width of the widest word of text in style at textScaler (pass the context's, MediaQuery.textScalerOf): below it, Flutter breaks a word in two.

Implementation

double comicWidestWord(
  String text,
  TextStyle style, {
  TextScaler textScaler = TextScaler.noScaling,
}) {
  var widest = 0.0;
  for (final word in text.split(RegExp(r'[\s/-]+'))) {
    if (word.isEmpty) continue;
    final tp = TextPainter(
      text: TextSpan(text: word, style: style),
      textDirection: TextDirection.ltr,
      maxLines: 1,
      textScaler: textScaler,
    )..layout();
    if (tp.width > widest) widest = tp.width;
    tp.dispose();
  }
  return widest;
}