0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Flutter】Text ウィジェットのサイズを計算する

Posted at

レイアウトの都合で実際にウィジェットを配置するまえにTextウィジェットのサイズが知りたいことがあります。
その場合はこのようにTextPainterを使ってサイズを取得することができます。

  Size getTextSize(String text) {
    final style = const TextStyle(
      fontSize: 12,
      letterSpacing: 0.67,
      fontWeight: FontWeight.w300,
    );
    final TextPainter textPainter = TextPainter(
        text: TextSpan(text: text, style: style),
        textAlign: TextAlign.start,
        // maxLines: 1,
        textDirection: TextDirection.ltr)
      ..layout(minWidth: 0, maxWidth: 300);
    return textPainter.size;
  }

参考
https://stackoverflow.com/questions/52659759/how-can-i-get-the-size-of-the-text-widget-in-flutter

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?