getTextSize method

Size getTextSize(
  1. String text,
  2. TextStyle style
)

The getTextSize function calculates and returns the size of the text when rendered with the specified style.

Args: text (String): The text parameter is a String that represents the text for which you want to calculate the size. style (TextStyle): The style parameter in the getTextSize method is of type TextStyle. It is used to specify the styling properties for the text that will be measured. This can include properties such as font size, font weight, color, and more. The TextStyle class in Flutter allows you

Returns: The Size of the text after it has been styled and laid out using the provided TextStyle.

Implementation

Size getTextSize(String text, TextStyle style) {
  final TextPainter textPainter = TextPainter(
    text: TextSpan(text: text, style: style),
    maxLines: 1,
    textDirection: TextDirection.ltr,
  )..layout(minWidth: 0, maxWidth: double.infinity);
  return textPainter.size;
}