Flutter Multiline for text

2020-06-08 01:30发布

All I need is my text to be multi-line. Am giving the property of maxLines but its still getting RenderFlex overflowed error to the right as the next is not going to 2nd line,

      Align( alignment: Alignment.bottomCenter,
      child: new ButtonBar(
        alignment: MainAxisAlignment.center,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(20.0),
            child: new Text(
              "This is a very bigggggggg text !!!",textDirection: TextDirection.ltr,
              style: new TextStyle(fontSize: 20.0, color: Colors.white),
              maxLines: 2,
            ),
          )
        ],
      ),
    )

6条回答
再贱就再见
2楼-- · 2020-06-08 01:57

try this:

Expanded(            
    child: Text(
      'a long text,
      overflow: TextOverflow.clip,
    ),
),

in my implementation I put this inside a row and surrounded it with sized boxes on each side so that I have some space between my elements, why using expanded you may ask, well it's used to take as much space as possible so the text would look good in portrait or landscape mode.

and here is a full example on how to make it even react vertically not just horizontally:

Card(
  child: InkWell(
    onTap: (){},
    child: Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        children: <Widget>[
          SizedBox(
            height: 70, // default\minimum height
          ),
          Container(
            height: 44,
            width: 44,
            decoration: BoxDecoration(
                color: Colors.blue,
                borderRadius: BorderRadius.all(Radius.circular(22))),
          ),
          SizedBox(
            width: 15,
          ),
          Expanded(
            child: Text(
              'the very long title',
              overflow: TextOverflow.clip,
            ),
          ),
          SizedBox(
            width: 10,
          ),
          Text(
            'value', //some other text in the end of the card or maybe an icon instead
          ),
          SizedBox(
            width: 30,
          ),
        ],
      ),
    ),
  ),
)
查看更多
Melony?
3楼-- · 2020-06-08 02:00

Maybe try using TextField:

new TextField(
  keyboardType: TextInputType.multiline,
  maxLines: 2,
)
查看更多
干净又极端
4楼-- · 2020-06-08 02:07

Just wrap your text widget with Expended as below

    Expanded(
      child: Text("data", maxLines: 4,
        overflow: TextOverflow.ellipsis,
        textDirection: TextDirection.rtl,
        textAlign: TextAlign.justify,),
    ),
查看更多
兄弟一词,经得起流年.
5楼-- · 2020-06-08 02:14

You can use this trick. Text( ''' This is very big text''' ), Just wrap a text around three single quotes and flutter will format a text as per its length . No need to use max lines and text field.

查看更多
放我归山
6楼-- · 2020-06-08 02:17

I think you forgot to add overflow type.

You can use something like this:

Text(
     "TOP ADDED",
     textAlign: TextAlign.justify,
     overflow: TextOverflow.ellipsis,
     style: TextStyle(fontSize: 18.0),
     maxLines: 2,)
查看更多
我只想做你的唯一
7楼-- · 2020-06-08 02:18

Make sure that the parent widget limits your text's width, and then use overflow and maxlines, e.g.:

Container(
  width: 150,
  child: Text(
    "This text is very very very very very very very very very very very very very very very very very very very very very very very very very long",
    overflow: TextOverflow.ellipsis,
    maxLines: 5,
  ),
),
查看更多
登录 后发表回答