gpt4 book ai didi

flutter - 如何设计一个自动在 flutter 中自动换行的Text Widget?

转载 作者:行者123 更新时间:2023-12-03 03:25:25 25 4
gpt4 key购买 nike

我想设计一个自动换行的文本小部件,当文本长度超过最大长度时,它将使用省略号。

以下代码可以达到类似的效果。

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

enter image description here

但是它必须指定 maxLines,我不需要这个,我需要小部件根据父对象的大小自动设置 maxLines

如何改善代码?

最佳答案

您可以尝试以下方法:

@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,
),
),
),
);
}

输出:

enter image description here

关于flutter - 如何设计一个自动在 flutter 中自动换行的Text Widget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58298559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com