简体   繁体   English

如何设计一个在 flutter 中自动换行的文本小部件?

[英]How to design a Text Widget that wraps automatically in flutter?

I want to design a text widget that wraps automatically, and it uses an ellipsis when the text length exceeds the maximum.我想设计一个自动换行的文本小部件,当文本长度超过最大值时它使用省略号。

The following code can achieve a similar effect.下面的代码可以达到类似的效果。

Container(
  color: Colors.red,
  child: Text(
    'DENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATIONDENTIFICATION',
    overflow: TextOverflow.ellipsis,
    maxLines: 6,
  ),
),

在此处输入图像描述

But it must specify maxLines , I don't want this, I need the widget to automatically set the maxLines based on the size of parent.但它必须指定maxLines ,我不想要这个,我需要小部件根据父级的大小自动设置maxLines

How can I improve the code?如何改进代码?

You can try this approach:您可以尝试这种方法:

@override
Widget build(BuildContext context) {
  String longText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
  double size = 100, fontSize = 16; 

  return Scaffold(
    appBar: AppBar(),
    body: Padding(
      padding: const EdgeInsets.all(20.0),
      child: SizedBox(
        height: size,
        child: Text(
          longText,
          style: TextStyle(fontSize: fontSize, height: 1),
          overflow: TextOverflow.ellipsis,
          maxLines: size ~/ fontSize,
        ),
      ),
    ),
  );
}

Output: Output:

在此处输入图像描述

Simply remove maxLines from your code, and your parent widget will handle it accordingly assuming the fact the constraints passed to Text are bounded, for instance in this example, I used SizedBox with height: 100 , and output is exactly what you want:只需从您的代码中删除maxLines ,您的父小部件将相应地处理它,假设传递给Text的约束是有界的,例如在这个例子中,我使用了height: 100SizedBox ,而 output 正是您想要的:

Edit:编辑:

SizedBox(
  height: 100,
  child: Text(
    'DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...DENTIFICATIOND...',
    overflow: TextOverflow.ellipsis,
    maxLines: 1000, // you can freely use any value here
  ),
)

Output: Output:

在此处输入图像描述

There are some known issues with Text wrapping, specially with the ellipsis if you don't specify the maxLines: Flexible text overflow ellipsis (softWrap false)文本换行存在一些已知问题,特别是省略号(如果您未指定 maxLines):灵活文本溢出省略号 (softWrap false)

But, you could do something like this to determine the maxLines according to the parent widget:但是,您可以执行以下操作来根据父小部件确定 maxLines:

SizedBox(
  height: 40.0,
  child: LayoutBuilder(
    builder: (BuildContext context, BoxConstraints constraints) {
      double fontSize = 16.0;
      return Text(
        "DENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIOND",
        style: TextStyle(fontSize: fontSize, height: 1.0),
        maxLines: constraints.maxHeight ~/ fontSize,
        overflow: TextOverflow.ellipsis,
      );
    },
  )

If you know the height of the parent, you could do something like this:如果你知道父母的身高,你可以这样做:

double fontSize = 16.0;
double height = 40.0;

SizedBox(
  height: height,
  child: Text(
    "DENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIONDDENTIFICATIOND",
    style: TextStyle(fontSize: fontSize, height: 1.0),
    maxLines: parentHeight ~/ fontSize,
    overflow: TextOverflow.ellipsis,
  )

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

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