颤动 - 在文本中插入溢出省略号

时间:2017-06-16 02:36:13

标签: dart flutter

我正在尝试创建一个中心文本具有最大大小的行,如果文本内容太大,则它适合大小。

我插入TextOverflow.ellipsis属性以缩短文本并插入三重点...,但它不起作用。

main.dart

import 'package:flutter/material.dart';

void main() {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) => new Scaffold(
    appBar: new AppBar(
      backgroundColor: new Color(0xFF26C6DA),
    ),
    body: new ListView  (
      children: <Widget>[
        new Card(
          child: new Container(
            padding: new EdgeInsets.symmetric(horizontal: 16.0, vertical: 18.0),
            child: new Row(
              children: <Widget>[
                new Container(
                  padding: new EdgeInsets.only(right: 24.0),
                  child: new CircleAvatar(
                    backgroundColor: new Color(0xFFF5F5F5),
                    radius: 16.0,
                  )
                ),
                new Container(
                  padding: new EdgeInsets.only(right: 13.0),
                  child: new Text(
                    'Text lar...',
                    overflow: TextOverflow.ellipsis,
                    style: new TextStyle(
                      fontSize: 13.0,
                      fontFamily: 'Roboto',
                      color: new Color(0xFF212121),
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                new Container(
                  child: new Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: <Widget>[
                      new Row(
                        children: <Widget>[
                          new Text(
                            'Bill  ',
                            style: new TextStyle(
                              fontSize: 12.0,
                              fontFamily: 'Roboto',
                              color: new Color(0xFF9E9E9E)
                            ),
                          ),
                          new Text(
                            '\$ -999.999.999,95',
                            style: new TextStyle(
                              fontSize: 14.0,
                              fontFamily: 'Roboto',
                              color: new Color(0xFF212121)
                            ),
                          ),
                        ],
                      ),
                      new Row(
                        children: <Widget>[
                          new Text(
                            'Limit  ',
                            style: new TextStyle(
                              fontSize: 12.0,
                              fontFamily: 'Roboto',
                              color: new Color(0xFF9E9E9E)
                            ),
                          ),
                          new Text(
                            'R\$ 900.000.000,95',
                            style: new TextStyle(
                              fontSize: 14.0,
                              fontFamily: 'Roboto',
                              color: new Color(0xFF9E9E9E)
                            ),
                          ),
                        ],
                      ),
                    ]
                  )
                )
              ],
            ),
          )
        ),
      ]
    )
  );
}

结果:

enter image description here

预期:

enter image description here

18 个答案:

答案 0 :(得分:64)

您应该将Container包裹在Flexible中,让Row知道Container比其内在宽度更窄是可以的。 Expanded也可以使用。

screenshot

            new Flexible(
              child: new Container(
                padding: new EdgeInsets.only(right: 13.0),
                child: new Text(
                  'Text largeeeeeeeeeeeeeeeeeeeeeee',
                  overflow: TextOverflow.ellipsis,
                  style: new TextStyle(
                    fontSize: 13.0,
                    fontFamily: 'Roboto',
                    color: new Color(0xFF212121),
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),

答案 1 :(得分:20)

使用省略号

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

enter image description here


使用淡入淡出

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

enter image description here


使用剪辑

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

enter image description here

答案 2 :(得分:9)

您可以使用此代码片段来显示带有省略号的文本

Text(
    "Introduction to Very very very long text",
    maxLines: 1,
    overflow: TextOverflow.ellipsis,
    softWrap: false,
    style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
),

答案 3 :(得分:3)

    SizedBox(
                        width: width-100,
                        child: Text(
                          "YOUR LONG TEXT HERE...",
                          maxLines: 3,
                          overflow: TextOverflow.clip,
                          softWrap: true,
                          style: TextStyle(
                            fontWeight:FontWeight.bold,
                          ),
                        ),
                      ),

答案 4 :(得分:3)

答案很多,但是还会观察更多。

1。剪辑

剪辑溢出的文本以修复其容器。

    <body>
    <canvas height="400" width="400" id="myCanvas"></canvas>       
    <script>
        "use strict"
        var a=document.getElementById("myCanvas");
        var c=a.getContext("2d");
        var shipX=180;
        var speed=1;
        c.fillStyle="white";
        c.beginPath();
        c.rect(shipX,350,40,30);
        c.fill();
        c.closePath();
        
        window.onkeydown=function(e){
            if(e.keyCode==37){
                 speed=-1;
                 window.setInterval(moveShip,17);
            }
            if(e.keyCode==39){
                speed=1;
                window.setInterval(moveShip,17);
            }
        }
        
        window.onkeyup=function(e){
            if(e.keyCode==37){
                speed=0;
            }
            if(e.keyCode==39){
                speed=0;
            }
        }
        
        
        function moveShip(){
            shipX+=speed; 
            c.clearRect(0,350,400,50);
            c.fillStyle="white";
            c.beginPath();
            c.rect(shipX,350,40,30);
            c.fill();
            c.closePath();
    }
        
    </script>
</body>

输出:

enter image description here

2。淡入淡出

使溢出的文本变为透明。

SizedBox(
          width: 120.0,
          child: Text(
            "Enter Long Text",
            maxLines: 1,
            overflow: TextOverflow.clip,
            softWrap: false,
            style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0),
          ),
        ),

输出:

enter image description here

3。省略号

使用省略号表示文本已溢出。

SizedBox(
          width: 120.0,
          child: Text(
            "Enter Long Text",
            maxLines: 1,
            overflow: TextOverflow.fade,
            softWrap: false,
            style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0),
          ),
        ), 

输出:

enter image description here

4。可见

将溢出的文本呈现在其容器之外。

SizedBox(
          width: 120.0,
          child: Text(
            "Enter Long Text",
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            softWrap: false,
            style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0),
          ),
        ),

输出:

enter image description here

请博客: https://medium.com/flutterworld/flutter-text-wrapping-ellipsis-4fa70b19d316

答案 5 :(得分:1)

我认为父容器需要给出适当大小的maxWidth。看起来文本框将填充上面给出的任何空间。

答案 6 :(得分:1)

用 Expanded() 包裹容器

Expanded (child: Container(
                  padding: new EdgeInsets.only(right: 24.0),
                  child: new CircleAvatar(
                    backgroundColor: new Color(0xFFF5F5F5),
                    radius: 16.0,
                  )
                ),
                new Container(
                  padding: new EdgeInsets.only(right: 13.0),
                  child: new Text(
                    'Text lar...',
                    overflow: TextOverflow.ellipsis,
                    style: new TextStyle(
                      fontSize: 13.0,
                      fontFamily: 'Roboto',
                      color: new Color(0xFF212121),
                      fontWeight: FontWeight.bold,
                    ),
                  ),
),

答案 7 :(得分:1)

你可以那样做

Expanded(
   child: Text(
   'Text',
   overflow: TextOverflow.ellipsis,
   maxLines: 1
   )
)

答案 8 :(得分:1)

首先,将RowColumn包装在FlexibleExpanded小部件中 然后

Text(
    'your long text here',
    overflow: TextOverflow.fade,
    maxLines: 1,
    softWrap: false,
    style: Theme.of(context).textTheme.body1,
)

答案 9 :(得分:0)

使用省略号表示文本已溢出。

SizedBox(
              width: 120.0,
              child: Text(
                "Enter Long Text",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                softWrap: false,
                style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0),
              ),
            ),

答案 10 :(得分:0)

如果仅将文本作为一列的子项放置,这是使文本自动换行的最简单方法。假设您没有进行任何更复杂的操作。在那种情况下,我认为您将创建自己认为合适的容器大小,并在其中放置另一列,然后输入文本。这似乎很好。容器希望缩小到其内容的大小,这似乎与包装自然冲突,这需要更多的努力。

Column(
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[
    Text('This long text will wrap very nicely if there isn't room beyond the column\'s total width and if you have enough vertical space available to wrap into.',
      style: TextStyle(fontSize: 16, color: primaryColor),
      textAlign: TextAlign.center,),
  ],
),

答案 11 :(得分:0)

根据您的情况,我认为这是下面的最佳方法。

final maxWidth = MediaQuery.of(context).size.width * 0.4;
Container(
        textAlign: TextAlign.center),
        child: Text('This is long text',
        constraints: BoxConstraints(maxWidth: maxWidth),
),

答案 12 :(得分:0)

一种解决文本小部件在一行内溢出的方法,例如,聊天消息可能是很长的一行。您可以创建一个容器和其中包含maxWidth的BoxConstraint。

            Container(
              constraints: BoxConstraints(maxWidth: 200),
                child: Text(
                  (chatName == null) ? " ": chatName,
                  style: TextStyle(
                      fontWeight: FontWeight.w400,
                      color: Colors.black87,
                      fontSize: 17.0),
                )
            ),

答案 13 :(得分:0)

包装其子元素在行或列中的包装,请包装您的列或行是新的Flexible();

https://github.com/flutter/flutter/issues/4128

答案 14 :(得分:0)

SizedBox(
    width: 200.0,
    child: Text('PRODUCERS CAVITY FIGHTER 50X140g',
                overflow: TextOverflow.ellipsis,
                style: Theme.of(context).textTheme.body2))

只需将其包装在一个小部件中即可,该小部件可以采用特定的宽度才能工作,否则它将采用父容器的宽度。

答案 15 :(得分:-1)

包装assorted_layout_widgets中有一个非常简单的类TextOneLine。

只需将您的文本放在该类中即可。

例如:

Row(
    crossAxisAlignment: CrossAxisAlignment.center,
    children: <Widget>[
      SvgPicture.asset(
        loadAsset(SVG_CALL_GREEN),
        width: 23,
        height: 23,
        fit: BoxFit.fill,
      ),
      SizedBox(width: 16),
      Expanded(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextOneLine(
              widget.firstText,
              style: findTextStyle(widget.firstTextStyle),
              textAlign: TextAlign.left,
            ),
            SizedBox(height: 4),
            TextOneLine(
              widget.secondText,
              style: findTextStyle(widget.secondTextStyle),
              textAlign: TextAlign.left,
            ),
          ],
        ),
      ),
      Icon(
        Icons.arrow_forward_ios,
        color: Styles.iOSArrowRight,
      )
    ],
  )

答案 16 :(得分:-1)

我以不同的方式解决了这个问题

  static String makeSort(value, characterLength) {
    return value != null
        ? value.length > characterLength
            ? value.substring(0, characterLength) + "...."
            : value
        : "";
  }

像这样声明一个全局函数并随时调用。

makeSort("This is a long Test",6)

我认为这会有所帮助。

答案 17 :(得分:-3)

尝试:

 Expanded(
  child: Container(
      child: Text('OVER FLOW TEST TEXTTTT',
          overflow: TextOverflow.fade)),
),

这将显示OVER FLOW。如果有溢出,将对其进行处理。