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

前端 未结 5 749
不思量自难忘°
不思量自难忘° 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:50

    from inspiring of the Günter Zöchbauer

    List _calcLastLineEnd(String msg) {
      // self-defined constraint
      final constraints = BoxConstraints(
        maxWidth: 800.0, // maxwidth calculated
        minHeight: 30.0,
        minWidth: 80.0,
      );
      final richTextWidget =
          Text.rich(TextSpan(text: msg)).build(context) as RichText;
      final renderObject = richTextWidget.createRenderObject(context);
      renderObject.layout(constraints);
      final boxes = renderObject.getBoxesForSelection(TextSelection(
          baseOffset: 0, extentOffset: TextSpan(text: msg).toPlainText().length));
      bool needPadding = false, needNextline = false;
      if (boxes.length < 2 && boxes.last.right < 630) needPadding = true;
      if (boxes.length < 2 && boxes.last.right > 630) needNextline = true;
      if (boxes.length > 1 && boxes.last.right > 630) needNextline = true;
      return [needPadding, needNextline];
    }
    

提交回复
热议问题