Flutter Multiline for text

前端 未结 8 885
春和景丽
春和景丽 2021-02-06 22:23

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 i

相关标签:
8条回答
  • 2021-02-06 22:47

    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,)
    
    0 讨论(0)
  • 2021-02-06 22:48

    Best way to handle this:

    Expanded(
    child: Text(document['content'],
    textAlign: TextAlign.start,
    overflow: TextOverflow.ellipsis,
    maxLines: 20,
    ))
    
    0 讨论(0)
  • 2021-02-06 22:57

    Maybe try using TextField:

    new TextField(
      keyboardType: TextInputType.multiline,
      maxLines: 2,
    )
    
    0 讨论(0)
  • 2021-02-06 22:58

    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.

    0 讨论(0)
  • 2021-02-06 22:59

    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,
              ),
            ],
          ),
        ),
      ),
    )
    
    0 讨论(0)
  • 2021-02-06 23:01

    Short answer

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

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

    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.

    Container(
      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,
      ),
    ),
    
    0 讨论(0)
提交回复
热议问题