Skip to content

Instantly share code, notes, and snippets.

@YusufAbdelaziz
Created July 18, 2021 22:51
Show Gist options
  • Save YusufAbdelaziz/8954dc761b21de215635053d645beaf0 to your computer and use it in GitHub Desktop.
Save YusufAbdelaziz/8954dc761b21de215635053d645beaf0 to your computer and use it in GitHub Desktop.
A method to know the width of a text before rendering it.
/// Finds the width of a text before rendering it.
Size findTextSize(
{required String text,
TextStyle style = textStyle,
required BuildContext context,
TextDirection textDirection = TextDirection.rtl}) {
final Size size = (TextPainter(
text: TextSpan(text: text, style: style),
maxLines: 5,
textScaleFactor: MediaQuery.of(context).textScaleFactor,
textDirection: textDirection)
..layout(minWidth: 0, maxWidth: double.infinity))
.size;
return size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment