Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter wrap text instead of overflow

When creating a Text widget with a long string in Flutter, it wraps its text when put directly in a Column. However, when it is inside Column-Row-Column, the text overflows the side of the screen.

How do I wrap text inside a Column-Row-Column? And what is the reason for this difference? It would seem logical to me that any child of the upper column would have the same width? Why is the width unbounded?

I tried putting the text in Expanded, Flexible, Container and FittedBox, based on other answers, but it leads to new errors I don't understand.

Example:

MaterialApp(   title: 'Text overflow',   home: Scaffold(     appBar: AppBar(title: Text("MyApp"),),     body: Column(       children: <Widget>[         Row(           children: <Widget>[             // The long text inside this column overflows. Remove the row and column above this comment and the text wraps.             Column(               children: <Widget>[                 Text("Short text"),                 Text("Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. ")               ],             ),           ],         ),       ],     ),   ),  ) 
like image 429
Jesse de Wit Avatar asked Feb 11 '19 15:02

Jesse de Wit


People also ask

How do I stop my text from overflowing flutters?

To overcome the overflow error, wrap the Text widget inside the Expanded widget. The Expanded widget allows the Text widget to grow and fill the available space.

What is soft wrap in Flutter?

softWrap. Whether the text should break at soft line breaks. If false, the glyphs in the text will be positioned as if there was unlimited horizontal space.

How do you wrap text in multiple lines in Flutter?

you have to wrap your text into a SizedBox and then you can also set multiple lines by editing the maxLines property of the Text widget: Example: SizedBox( //You can define in as your screen's size width, //or you can choose a double //ex: //width: 100, width: MediaQuery. of(context).


2 Answers

Row and Column are Flex widget and don't scroll, if there is not enough space flutter raises an overflow error.

If this occurs an Expanded or a Flexible widget may be used to wrap a long text.

In the docs it is not clearly stated, but beyond expanding to fill the available space in the main axis, Expanded and Flexible wraps in the cross-axis direction.

The long story

A step by step approach may help to understand the problem.

First, consider this scenario:

class MyApp extends StatelessWidget {   // This widget is the root of your application.   @override   Widget build(BuildContext context) {     return MaterialApp(       title: 'Text overflow',       home: Scaffold(         appBar: AppBar(           title: Text("MyApp"),         ),         body: Column(           children: List.generate(100, (idx) => Text("Short text"))         ),       ),     );   } } 

It is a column widget that overflows on vertical direction as clearly reported by flutter:

I/flutter ( 8390): The following message was thrown during layout: I/flutter ( 8390): A RenderFlex overflowed by 997 pixels on the bottom. I/flutter ( 8390):  I/flutter ( 8390): The overflowing RenderFlex has an orientation of Axis.vertical. 

Now, a Row inside a Column:

class MyApp extends StatelessWidget {   // This widget is the root of your application.   @override   Widget build(BuildContext context) {     return MaterialApp(       title: 'Text overflow',       home: Scaffold(         appBar: AppBar(title: Text("MyApp"),),         body: Column(           children: <Widget>[             Row(               children: <Widget>[                 Text(String.fromCharCodes(List.generate(100, (i) => 65)))               ],             ),           ],         ),       ),     );   } } 

Now the overflow problem appears on the right side.

I have inserted a Row in a Column just to resemble your case, but the exact same problem emerge if you use a simple Row widget: Row and Column are both Flex widgets:

  • they layout their children in one direction;
  • they do not scroll, so if the children occupy more space than available an overflow error is raised;

The Expanded widget

Consider this layout, a row with two items, stiched togheter

Column(   children: <Widget>[     Row(       children: <Widget>[Text('AAAA'), Text('ZZZZ')],     ),   ], ), 

Now the first item Expanded to fill all the available space:

Column(   children: <Widget>[     Row(       children: <Widget>[         Expanded(child: Text('AAAA')),         Text('ZZZZ')],     ),   ], ), 

Finally, when you expand a very long string you will notice that the text wraps in the cross-axis direction:

Column(   children: <Widget>[     Row(       children: <Widget>[         Expanded(child: Text(String.fromCharCodes(List.generate(100, (i) => 65)))),         Text('ZZZZ')],     ),   ], ), 

like image 142
attdona Avatar answered Sep 22 '22 15:09

attdona


You need to wrap the last Column with - Expanded or Flexible widget.

That Way Column can take up the required available space for the text.

body: Column(         children: <Widget>[           Row(             children: <Widget>[               // The long text inside this column overflows. Remove the row and column above this comment and the text wraps.               Expanded(                 child: Column(                   children: <Widget>[                     Text("Short text"),                     Text(                         "Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. ")                   ],                 ),               ),             ],           ),         ],       ), 
like image 36
anmol.majhail Avatar answered Sep 25 '22 15:09

anmol.majhail