getLayoutWidth method

double getLayoutWidth(
  1. BuildContext context, {
  2. TextStyle? explicitTextStyle,
})

Implementation

double getLayoutWidth(BuildContext context, {TextStyle? explicitTextStyle}) {
  assert(this.style != null || explicitTextStyle != null,
      "A TextStyle needs to be provided");

  if (this.data == null || this.data == "") return 0;

  var textWidget = Text.rich(
      TextSpan(text: this.data, style: explicitTextStyle ?? this.style));
  var textWidgetBuild = textWidget.build(context) as RichText;
  var renderObject = textWidgetBuild.createRenderObject(context)
    ..layout(BoxConstraints());
  var boxes = renderObject.getBoxesForSelection(
      TextSelection(baseOffset: 0, extentOffset: this.data!.length));
  var textWidth = boxes.last.right;

  return textWidth;
}