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.
Asked
Active
Viewed 7,134 times
2 Answers
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
-
This works for me, I was looking for a way that the widget might have a build in setting to limit the max length. – punjabi4life Aug 12 '19 at 23:59
-
instead of '...' import `import 'package:charcode/html_entity.dart';` and use `String.fromCharCode($hellip)` – Kirill Karmazin Jan 31 '22 at 14:37
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