In android, we can set max lines for text view like this:
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="5" />
However in flutter, we can do that by adding max height constrains, but we need to know what exactly the height is.
Is there any way we can easily specify the max lines for Text/RichText?
Simple using the maxLines
property
Text("Some large text", maxLines: 2)
And you can use overflow: TextOverflow.ellipsis
to insert ...
overflow ellipsis in text
Text("Some large text", maxLines: 2, overflow: TextOverflow.ellipsis)
Edit 2021 - it's possible now
(Hint - Optional but recommended is to set some overflow with maxLines)
Text(
'My text',
maxLines: 4,
overflow: TextOverflow.ellipsis,
),
RichText(
text: TextSpan(children: ....),
maxLines: 4,
overflow: TextOverflow.ellipsis,
),
I believe it's supported now:
RichText(
maxLines: 2,
overflow: TextOverflow.ellipsis, // TextOverflow.clip // TextOverflow.fade
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With