Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set max lines for Text or RichText?

Tags:

flutter

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?

like image 768
Wei Song Avatar asked Dec 14 '16 22:12

Wei Song


3 Answers

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)
like image 144
Pablo Cegarra Avatar answered Oct 24 '22 03:10

Pablo Cegarra


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,
        ),
like image 20
mpcomplete Avatar answered Oct 24 '22 03:10

mpcomplete


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!'),
        ],
    ),
)
like image 23
Elroy Avatar answered Oct 24 '22 02:10

Elroy