如何在 Flutter 中使用带有多行文本的省略号溢出

mat*_*twr 6 dart flutter flutter-web

当我配置最多 1 行且溢出设置为省略号的 Text 小部件时,该小部件显示正确。

 Text(
    "Last summer, I was working on a prototype for an AR app that would allow users to create
     virtual objects in real world spaces. I carried out the work using the Unity3D game engine.
     My last commit to the repository was five months ago.",
     maxLines: 1,
     overflow: TextOverflow.ellipsis
     )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

但是,当我将最大行数设置为大于 1 的任何数字时,行数被正确限制,但不会显示溢出省略号。

在此处输入图片说明

 Text(
    "Last summer, I was working on a prototype for an AR app that would allow users to create
     virtual objects in real world spaces. I carried out the work using the Unity3D game engine.
     My last commit to the repository was five months ago.",
     maxLines: 2,
     overflow: TextOverflow.ellipsis
     )
Run Code Online (Sandbox Code Playgroud)

当为 maxLines 设置多于一行时,如何配置 Text 小部件以显示溢出省略号?

小智 -1

在您的示例中,最大行数为 3,但文本仅显示 2 行文本。这意味着您没有为最大行提供足够的高度(例如容器的高度)。

因此,您可以为其提供更多空间或将最大行数减少到 2 或将文本小部件放置在 Flex 小部件中(例如 ListView,...)

你可以测试一下这个,看看我的意思

        Container(
          height: 100,
          child: Text(
            "Last summer, I was working on a prototype for an AR app that would allow users to create virtual objects in real world spaces. I carried out the work using the Unity3D game engine. My last commit to the repository was five months ago.",
            maxLines: 3,
            overflow: TextOverflow.ellipsis,
          ),
        ),
Run Code Online (Sandbox Code Playgroud)