357

I want wrap text as text grows. I searched through and tried wrap with almost everything but still text stays one line and overflows from the screen. Does anyone know how to achieve this? Any help is highly appreciated!

Positioned(
    left: position.dx,
    top: position.dy,
    child: new Draggable(
      data: widget.index,
      onDragStarted: widget.setDragging,
      onDraggableCanceled: (velocity, offset) {
        setState(() {
          position = offset;
          widget.secondCallback(offset, widget.index);
          widget.endDragging();
        });
      },
      child: new GestureDetector(
        onTap: () {
          widget.callback(widget.caption, widget.index);
        },
        child: new Text(
            widget.caption.caption,
            style: new TextStyle(
              color: widget.caption.color,
              fontSize: widget.caption.fontSize,
            ),
          ),
      ),
      feedback: new Material(
        type: MaterialType.transparency,
        child: new Text(
          widget.caption.caption,
          style: new TextStyle(
              color: widget.caption.color,
              fontSize: widget.caption.fontSize),
          softWrap: true,
        ),
      ),
    ));
0

17 Answers 17

612

The Flexible does the trick

new Container(
       child: Row(
         children: <Widget>[
            Flexible(
               child: new Text("A looooooooooooooooooong text"))
                ],
        ));

This is the official doc https://flutter.dev/docs/development/ui/layout#lay-out-multiple-widgets-vertically-and-horizontally on how to arrange widgets.

Remember that Flexible and also Expanded, should only be used within a Column, Row or Flex, because of the Incorrect use of ParentDataWidget.

The solution is not the mere Flexible

8
  • 3
    Using Row and column for a single child widget isnt a good Idea Commented Apr 13, 2020 at 3:52
  • 2
    This also can be substituted with Expanded(), but Expanded() makes the child fill the rest of the available space. It can be observed in the Flutter's Inspector tool, to understand better. Commented Dec 1, 2020 at 20:17
  • 1
    I used the Flexible() widget and it wordked. You should edit your answer then it would be the accepted solution.
    – Riad Rekab
    Commented Dec 3, 2020 at 13:18
  • The Flexible does the trick, but the Container allows you to add a padding. This is the official doc flutter.dev/docs/development/ui/… on how to arrange widgets, as @KonstantinKozirev said you can use Expanded as well.
    – Fiury
    Commented Dec 4, 2020 at 14:29
  • 1
    Unfortunately, both Flexible and Expanded cause the text to take up all the remaining horizontal space in a row, even when after wrapping the bounding box around the text could be "shrunk" to fit the text. This may lead to an unexpected result if you're trying to center, within a column, a row containing wrapping text. Commented Dec 24, 2022 at 1:29
153

In a project of mine I wrap Text instances around Containers. This particular code sample features two stacked Text objects.

Here's a code sample.

    //80% of screen width
    double c_width = MediaQuery.of(context).size.width*0.8;

    return new Container (
      padding: const EdgeInsets.all(16.0),
      width: c_width,
      child: new Column (
        children: <Widget>[
          new Text ("Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 ", textAlign: TextAlign.left),
          new Text ("Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2", textAlign: TextAlign.left),
        ],
      ),
    );

[edit] Added a width constraint to the container

9
  • 1
    Thank you for the help but still not working. I think it's something to do with stack or positioned or draggable. Because hierarchy is stack -> positioned -> draggable. Am I right?
    – Daibaku
    Commented Aug 20, 2018 at 13:15
  • 1
    Thank you so much it worked perfectly. You just made my day!
    – Daibaku
    Commented Aug 20, 2018 at 13:35
  • 81
    Try the Flexible widget instead. Hardcoding containers width should not be necessary.
    – Boni2k
    Commented Aug 20, 2018 at 13:47
  • 2
    it didn’t work i tried so many times with it. Do you have any idea?
    – Daibaku
    Commented Aug 20, 2018 at 14:53
  • 2
    Perl forlks would say: "There's more than one way to do it". It served me well at the time, when Flutter was fairly new-ish. I'm glad there's now an official path. Commented Feb 22, 2021 at 12:49
129

Using Ellipsis

Text(
  "This is a long text",
  overflow: TextOverflow.ellipsis,
),

enter image description here


Using Fade

Text(
  "This is a long text",
  overflow: TextOverflow.fade,
  maxLines: 1,
  softWrap: false,
),

enter image description here


Using Clip

Text(
  "This is a long text",
  overflow: TextOverflow.clip,
  maxLines: 1,
  softWrap: false,
),

enter image description here


Note:

If you are using Text inside a Row, you can put above Text inside Expanded like:

Expanded(
  child: AboveText(),
)
2
  • In my case, this line caused the text to not wrap to next line and instead was just getting clipped: overflow: TextOverflow.ellipsis Changing it to TextOverflow.fade or removing the overflow tag completely solved the problem. Weird, but I guess if I need it to wrap, I shouldn't be using overflow anyways.
    – Hanzyusuf
    Commented Mar 4, 2023 at 5:59
  • I wish you would explain further why Expanded solves the Row issue. Expanded does expand the Text to the full with of the Row giving it a size it can wrap on, but why does it fail this in the first place? If the Text was otherwise 0 width, it shouldn't be visible at all. Commented May 27, 2024 at 23:28
110

Use Expanded

 Expanded(
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Text(_name, style: Theme.of(context).textTheme.subhead),
                new Container(
                  margin: const EdgeInsets.only(top: 5.0),
                  child: new Text(text),
                ),
              ],
            ),
2
  • 3
    This. You could also use flexible without a flex (i.e. flex set to 1) in order to fill 100% of the available space. Commented Jan 14, 2019 at 15:25
  • This should be the accepted answer. Reason: Google advertises the same solution: codelabs.developers.google.com/codelabs/flutter/#8 (see Chapter Wrap long lines)
    – DarkTrick
    Commented May 7, 2022 at 8:18
68

If it's a single text widget that you want to wrap, you can either use Flexible or Expanded widgets.

Expanded(
  child: Text('Some lengthy text for testing'),
)

or

Flexible(
  child: Text('Some lengthy text for testing'),
)

For multiple widgets, you may choose Wrap widget. For further details checkout this

2
  • 7
    Both solutions are not working if Text widget is a child of Column. Any other suggestion? Thanks.
    – Kamlesh
    Commented May 19, 2021 at 8:33
  • 7
    Recommending Flexible or Expanded without a single mention of Column / Row is, in my opinion, harmful.
    – cubuspl42
    Commented Jun 17, 2021 at 10:41
23

Try Wrap widget to wrap text as text grows:

Example:

Wrap(
  direction: Axis.vertical, //Vertical || Horizontal
  children: <Widget>[
    Text(
      'Your Text',
      style: TextStyle(fontSize: 30),
    ),
    Text(
      'Your Text',
      style: TextStyle(fontSize: 30),
    ),
  ],
),
1
  • 11
    This solution is not working if Text widget is a child of Column. Any other suggestion? Thanks
    – Kamlesh
    Commented May 19, 2021 at 8:35
16

You Can Wrap your widget with Flexible Widget and than you can set property of Text using overflow property of Text Widget. you have to set TextOverflow.clip for example:-

Flexible
(child: new Text("This is Dummy Long Text",
 style: TextStyle(
 fontFamily: "Roboto",
 color: Colors.black,
 fontSize: 10.0,
 fontWeight: FontWeight.bold),
 overflow: TextOverflow.clip,),)

hope this help someone :)

3
  • This should be the correct answer. Less code and up to the point (actual purpose of the widget) Commented Jul 31, 2020 at 15:54
  • 4
    Flexible is just dismiss all my text , not showing anything Commented Oct 18, 2020 at 5:38
  • This solution worked perfectly for me! Simple and powerful! Thank you!
    – RedPelle
    Commented Aug 23, 2022 at 10:36
16
Container(
  color: Color.fromRGBO(224, 251, 253, 1.0),
  child: ListTile(
    dense: true,
    title: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        RichText(
          textAlign: TextAlign.left,
          softWrap: true,
          text: TextSpan(children: <TextSpan>
          [
            TextSpan(text: "hello: ",
                style: TextStyle(
                    color: Colors.black, fontWeight: FontWeight.bold)),
            TextSpan(text: "I hope this helps",
                style: TextStyle(color: Colors.black)),
          ]
          ),
        ),
      ],
    ),
  ),
),
0
8

You can use Flexible, in this case the person.name could be a long name (Labels and BlankSpace are custom classes that return widgets) :

new Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
   new Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        new Flexible(
            child: Labels.getTitle_2(person.name,
            color: StyleColors.COLOR_BLACK)),
        BlankSpace.column(3),
        Labels.getTitle_1(person.likes())
      ]),
    BlankSpace.row(3),
    Labels.getTitle_2(person.shortDescription),
  ],
)
6

Using SizedBox to hardcode the width, and softWrap to make new line of Text

SizedBox(
   width: 400,
   child: Text(
      "Really long text",
      softWrap: true,              
   ),
)
2
  • 1
    Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community? Commented Aug 15, 2023 at 6:35
  • well done, thank you. Commented Sep 11, 2024 at 9:54
3

You can do it using the auto_size_text package:

 Container(
  child: ConstrainedBox(
    constraints: BoxConstraints(
      minWidth: 200.0,
      maxWidth: 200.0,
      minHeight: 50.0,
      maxHeight: 150.0,
    ),
    child: AutoSizeText(
      "Your Text",
      style: TextStyle(fontSize: 20.0),
    ),
  ),
);

Example: Example:

You can further constrain the text by setting maxLines or specify presetFontSizes if you wish to restrict specific font sizes.

1

For Anybody who tried using an Expanded or Flexible widget and it didn't work, the trick is to set your text's maxLines property to a given value > 1. Example:

Row(children: [
                  Expanded(
                    child: Container(
                      color: Colors.red,
                      child: Text(
                        product.description!,
                        textAlign: TextAlign.left,

                       //Set MAXimum lines
                        maxLines: 5,
                        style: TextStyles.caption1TextStyle
                            .copyWith(fontSize: TextStyles.bodySize),
                      ),
                    ),
                  ),
]);

1

Pure Flutter. Multiple lines and auto fit inside container.

Widget text = LayoutBuilder(builder: (context, constraints) {
      return FittedBox(
          fit: BoxFit.scaleDown,
          child: ConstrainedBox(
              constraints: BoxConstraints(maxWidth: constraints.maxWidth),
              child: RichText(
                text: TextSpan(
                  text: 'your long text here',
                ),
                textAlign: TextAlign.center,
              )));
    });
0

Very simple solution for a simple instance, use the softWrap property of the Text Widget.

With softWrap enabled: enter image description here

2
  • Post actual code instead of image Commented Jan 4, 2024 at 20:30
  • It's as simple as: const Text( 'A very long text. We need this text to move to another line instead of overflowing', softWrap: true, )
    – Quwaysim
    Commented Jan 5, 2024 at 19:40
0
Expanded(
         child: Text(
           'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.',
           style: TextStyle(fontSize: 58),
           softWrap: false,
           maxLines: 3,
         ),
       ),

Utilise Expanded widget to set TextOverflow.visible

for those who want this, thankks

0

Wrap Text with the FittedBox widget:

Container(
        height: 40,
        width: 40,
        color: Colors.blue,
        child: const FittedBox(
          fit: BoxFit.fill,
          child: Text('Hello world!', style: TextStyle(fontSize: 20)),
        ));
0
-1

wrapping text flutter

 Container(
            padding: EdgeInsets.all(20.0),
            child: Text(
              'This is a long text that needs to be wrapped to the next line because it cannot fit within the available space.',
              style: TextStyle(fontSize: 16.0),
              maxLines: 2, // Limit the text to 2 lines
              overflow: TextOverflow.ellipsis, // Use ellipsis (...) for overflow
              softWrap: true, // Enable text wrapping
            ),
          ),
        ),

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.