flutter - 如何将文本小部件长度固定为特定长度,然后应继续中断到下一行
问题描述
我有一个包含文本的文本小部件,文本不会溢出,但我想将其修复为特定长度然后多行。
因此,如果我将长度固定为 5,则立即超过 5 或达到 5,他应该中断到下一行
这是我的代码:
ListView.builder(
shrinkWrap: true,
itemCount: recentChats.length,
itemBuilder: (context, index) {
//final sender = recentChats[index].sender;
final auth = recentChats[index].sender.id;
final msg = recentChats[index].content;
//final timestamp = recentChats[index].time;
bool check = auth == 1 ? true : false;
return Padding(
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 10,
),
child: Column(
crossAxisAlignment: check
? CrossAxisAlignment.end
: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: check ? Colors.indigo : Colors.white,
borderRadius: check
? BorderRadius.only(
bottomLeft: Radius.circular(30),
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
)
: BorderRadius.only(
bottomRight: Radius.circular(30),
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: Text(
msg,
style: GoogleFonts.raleway(
textStyle: TextStyle(
color:
check ? Colors.white : Colors.black,
),
),
),
)
],
),
);
}),
我想要达到的目标:
我尝试使用 maxLine 但没有达到我想要的......
这就是我的代码得到的
解决方案
尝试将您的文本包装在ConstrainedBox
:
//...
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 50), //<-- set the max width here
child: Text(
msg,
style: GoogleFonts.raleway(
textStyle: TextStyle(
color: check ? Colors.white : Colors.black,
),
),
),
),
//...
推荐阅读
- go - 谷歌人员 API 无法获取电子邮件地址
- powershell - 上传到远程 SFTP,本地服务器上的一个文件夹与多个不同的文件上传到远程主机上的多个文件夹
- reactjs - 部署后用户开始收到`Unrecognized token "<"`
- css - 为什么父子不继承父的滚动条样式
- postgresql - linux系统中用于unixodbc的sqlplus命令行工具的替代品是什么?
- sql - 在databricks SQL中将字符串转换为日期
- c# - 无法编译从服务器下载的存储库
- javascript - 具有相同名称的动态多个输入
- flutter - 使用 Futurebuilder 创建 DropdownFormField Flutter?
- paypal - Exception management in Stripe payments