How can I get the size of the Text Widget in flutter

前端 未结 5 748
不思量自难忘°
不思量自难忘° 2020-11-29 01:19

I\'ve painted a shape for the background of my content of Text.

I want the background autoscale the Text, even the softWrap being true.

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 01:42

    I found another method without using the context:

    final constraints = BoxConstraints(
      maxWidth: 800.0, // maxwidth calculated
      minHeight: 0.0,
      minWidth: 0.0,
    );
    
    RenderParagraph renderParagraph = RenderParagraph(
      TextSpan(
        text: text,
        style: TextStyle(
          fontSize: fontSize,
        ),
      ),
      textDirection: ui.TextDirection.ltr,
      maxLines: 1,
    );
    renderParagraph.layout(constraints);
    double textlen = renderParagraph.getMinIntrinsicWidth(fontSize).ceilToDouble();
    

提交回复
热议问题