5

I am trying to find out how I can limit the amount of characters are show before the ellipsis is shown in a flutter text widget.

punjabi4life
  • 655
  • 1
  • 9
  • 22

2 Answers2

21

well this does not make sense a little bit to me, I mean if you want to limit amount of your text characters then "ellipsis" does not mean anything at all so just limit your text in a constant length

final text = 'hello stack overflow';
Text(text.length > 3 ? '${text.substring(0, 3)}...' : text);
Amir Ghezelbash
  • 2,195
  • 15
  • 24
0

It is a different problem but I will assume you do not know how to get the size of the text widget, here is how to get that size: How to get Widget size

Per how to compute when the text will overflow the bounds take the size of the widget and assign it to widget_width and proceed to use the following pseudo code as follows:

double widget_width = computeWidgetWidthAsShownInLinkAbove(context, text_widget);

String    sample = "This text is probably to long to fit";

TextStyle textstyle  = new TextStyle(fontSize: i);

int answer = 0;

for ( int I = 0; I < sample.length ; I = I + 1 ) {
  TextPainter textPainter = TextPainter(textDirection: TextDirection.ltr);

  textPainter.text = new TextSpan(
          text: sample.substring(0, I ), style: textstyle);

  textPainter.layout();

  if ( textPainter.width > widget_width ) {
    answer=I-1;  
    break;
  }
}
E.Bradford
  • 783
  • 7
  • 21