getTextPainter function

TextPainter getTextPainter(
  1. BuildContext context,
  2. String text,
  3. TextStyle textStyle
)

获取文本的宽度和高度

Implementation

TextPainter getTextPainter(BuildContext context, String text, TextStyle textStyle) {
  TextPainter textPainter = TextPainter(
      text: TextSpan(
        text: text,
        style: textStyle,
      ),
      textDirection: TextDirection.ltr,
      strutStyle: StrutStyle.fromTextStyle(textStyle, height: 1, forceStrutHeight: true),
      locale: Localizations.localeOf(context, nullOk: true),
      maxLines: 1,
      textHeightBehavior: TextHeightBehavior(applyHeightToFirstAscent: false, applyHeightToLastDescent: false))
    ..layout(maxWidth: double.infinity, minWidth: 0);
  return textPainter;
}