Wei Song
Wei Song

Reputation: 1627

How to set max lines for Text or RichText?

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?

Upvotes: 29

Views: 51562

Answers (4)

mpcomplete
mpcomplete

Reputation: 261

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,
        ),

Upvotes: 21

ZealousWeb
ZealousWeb

Reputation: 1751

Use simple snippet of code, you can achieve this:

Text("Text",
    style: TextStyle(
        fontFamily: "Avenir",
        color: Color(0xff0C64B1),
        fontSize: width * 0.045),
    maxLines: 10)

Let me know if you are facing any issues with above. I'd be happy to assist you.

Upvotes: -4

Pablo C. Garc&#237;a
Pablo C. Garc&#237;a

Reputation: 22394

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)

Upvotes: 54

Elroy
Elroy

Reputation: 1660

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!'),
        ],
    ),
)

Upvotes: 11

Related Questions