简体   繁体   English

如何解决 flutter 中的“文本溢出。椭圆”?

[英]How to solve for 'text overflow.ellipse' in flutter?

I am unable to make my text stop extending the screen and overflowing.我无法让我的文本停止扩展屏幕和溢出。 My code is as follows:我的代码如下:

class OrderTileDisplay extends StatelessWidget {
  final MenuOrderData orderItem;
  final editable;
  OrderTileDisplay(this.orderItem, {this.editable = true});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        GestureDetector(
          onTap: (){},
          child: Container(
            color: Colors.transparent,
            margin: EdgeInsets.symmetric(vertical: 4),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    Text(
                      orderItem.quantity.toString() + 'x',
                      style: Title16,
                    ),
                    SizedBox(
                      width: 8,
                    ),
                    Text(
                      orderItem.name, // OVERFLOWS WHEN IT IS A LONGER SENTENCE
                      style: Title18bold,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
                Row(
                  children: [
                    editable? Icon(Icons.edit, color: Colors.grey[200], size: 20,) : Container(),
                    SizedBox(width: 8,),
                    Text("£" + Provider.of<CP>(context).getTotalForItem(orderItem).toStringAsFixed(2),
                      style: Title18bold,),
                  ],
                ),
              ],
            ),
          ),
        ),
Container(... and other stuff)

I have tried putting Text inside a container then Expanded widget but I keep getting errors.我曾尝试将 Text 放入容器中,然后是 Expanded 小部件,但我不断收到错误消息。 Not sure how to solve it.不知道如何解决它。

Try to wrap your Row and Text inside a Flexible:尝试将您的RowText包装在 Flexible 中:

Flexible( //flexible here
  child: Row(
    children: [
      Text(
        orderItem.quantity.toString() + 'x',
        style: Title16,
      ),
      SizedBox(
        width: 8,
      ),
      Flexible( //and here
        child: Text(
          orderItem.name, // no more overflow
          style: Title18bold,
          overflow: TextOverflow.ellipsis, //working as expected
        ),
      ),
    ],
  ),
),

As the documentation states, TextOverflow says how overflowing text should be handled, but it will not work if it's parent don't have a fixed size.正如文档所述, TextOverflow说明了应该如何处理溢出的文本,但如果它的父级没有固定大小,它将不起作用。 In this case, Flexible is been used to restrict the total size of the text to prevent overflow, when it reaches a very large width.在这种情况下, Flexible用于限制文本的总大小以防止在达到非常大的宽度时溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM