Flutter Multiline for text

AndroidFlutterFlutter Layout

Android Problem Overview


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

Android Solutions


Solution 1 - Android

Short answer

All that is required for multi-line text, is that your Text() widgets' width is limited by a parent widget. For example:

SizedBox(
    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",
    ),
),

Long answer

Minimal working example + explanation:

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold( //Text widgets, multi-line or not, need a Scaffold widget somewhere up the widget tree.
        body: Row( //Widgets which help to display a list of children widgets are the 'culprit', they make your text widget not know what the maximum width is. In OP's example it is the ButtonBar widget.
          children: [
            Container( 
              width: 100, //This helps the text widget know what the maximum width is again! You may also opt to use an Expanded widget instead of a Container widget, if you want to use all remaining space.
              child: Center( //I added this widget to show that the width limiting widget doesn't need to be a direct parent.
                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",
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Extra

You also mentioned maxlines. This property limits the maximum amount of lines. If this is what you want, I recommend you also play with the overflow property.

SizedBox(
  width: 100,
  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",
    maxLines: 2,
    overflow: TextOverflow.ellipsis,
  ),
),

UPDATE

I came across this video on the official Flutter channel which explains this issue quite well. They recommend using a LimitedBox widget.

Solution 2 - Android

Just wrap your text widget with Expanded as below

    Expanded(
      child: Text('data', maxLines: 4,
        overflow: TextOverflow.ellipsis,
        textDirection: TextDirection.rtl,
        textAlign: TextAlign.justify,),
    ),

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

Use Expanded widget with column to achieve multiline text.

Expanded(child: 
 Column(crossAxisAlignment: CrossAxisAlignment.start ,
 children: [
            Text(food.title),
            Text(food.price)
           ]
))

Solution 6 - Android

Best way to handle this:

Expanded(
child: Text(document['content'],
textAlign: TextAlign.start,
overflow: TextOverflow.ellipsis,
maxLines: 20,
))

Solution 7 - Android

Just wrap the text widget in Flexible. As it only use space that it need. After maxLines, it just cut to ellipsis.

> If you want unlimited lines, just remove maxLines attribute. With the help of Flexible, it uses space which widget needs.

Flexible(
  child: Text('Long Text', maxLines: 3,
    overflow: TextOverflow.ellipsis,
    textAlign: TextAlign.justify,
  ),
),

Solution 8 - Android

Maybe try using TextField:

new TextField(
  keyboardType: TextInputType.multiline,
  maxLines: 2,
)

Solution 9 - Android

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.

Solution 10 - Android

wrap your Text widget with AutoSizeText widget and also Flexible widget.

  • AutoSizeText adjust your text size as per the screen size

  • Flexible control your widget not to overflow outside

    for get AutoSizeText you have to add dependency. auto_size_text: ^3.0.0 - Example

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVivek C AView Question on Stackoverflow
Solution 1 - AndroidTom OView Answer on Stackoverflow
Solution 2 - AndroidDanford KijaView Answer on Stackoverflow
Solution 3 - AndroidBasel AbuhadrousView Answer on Stackoverflow
Solution 4 - AndroidMohammed MahmoudView Answer on Stackoverflow
Solution 5 - AndroidMohd Danish KhanView Answer on Stackoverflow
Solution 6 - AndroidShahzad RasoolView Answer on Stackoverflow
Solution 7 - AndroidRehan AliView Answer on Stackoverflow
Solution 8 - Androiddragonfly02View Answer on Stackoverflow
Solution 9 - AndroidSaurabh pethaniView Answer on Stackoverflow
Solution 10 - AndroidHarihara dhamodaranView Answer on Stackoverflow