Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Multiline for text

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,             ),           )         ],       ),     ) 
like image 874
Vivek C A Avatar asked Dec 17 '18 09:12

Vivek C A


People also ask

How do you text the next line in Flutter?

\n is the new line character. Using this character, we can easily make a new line.

How do you wrap text in TextField Flutter?

Here's how you wrap text on overflow in Flutter:Step 1: Make sure your Text widget is inside the Row widget. Step 2: Wrap your Text widget inside the Expanded widget. Step 3: Run your app.


Video Answer


2 Answers

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.

like image 109
Tom O Avatar answered Oct 31 '22 17:10

Tom O


Just wrap your text widget with Expanded as below

    Expanded(       child: Text('data', maxLines: 4,         overflow: TextOverflow.ellipsis,         textDirection: TextDirection.rtl,         textAlign: TextAlign.justify,),     ), 
like image 39
Danford Kija Avatar answered Oct 31 '22 19:10

Danford Kija