如何动态调整文字大小

问题描述 投票:0回答:1

我正在创建一个列表,其中包含一些包含文本的图块,但是它们的大小不能太大以至于会破坏布局。因此,我仅从文本中获取一个子字符串,然后在其中添加“ ...”,以指示布局中隐藏了更多文本。问题是,某些文本处于大写锁定状态,这使文本具有不同的大小。这是图块的示例代码:

class _MessageTileState extends State<MessageTile> {
  @override
  Widget build(BuildContext context) {
    final colorGrey = Theme.of(context).unselectedWidgetColor;
    final screenSize = MediaQuery.of(context).size;

    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        messageIcon(widget.message),
        SizedBox(width: 16.0),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              widget.message.author.length > 38 ? widget.message.author.substring(0, 35) + '...' : widget.message.author,
              style: TextStyle(
                fontSize: screenSize.width * 0.033,
                fontWeight: FontWeight.w600,
                height: 1.6,
                color: widget.message.read ? colorGrey : null,
              ),
            ),
            Text(
              widget.message.title.length > 38 ? widget.message.title.substring(0, 35) + '...' : widget.message.title,
              style: TextStyle(
                fontSize: screenSize.width * 0.033,
                fontWeight: FontWeight.w600,
                height: 1.6,
                color: widget.message.read ? colorGrey : null,
              ),
            ),
            Text(
              widget.message.message.length > 38 ? widget.message.message.substring(0, 35).replaceAll('\n\n', ' ') + '...' : widget.message.message.replaceAll('\n\n', ' '),
              style: TextStyle(
                fontSize: screenSize.width * 0.033,
                height: 1.6,
                color: widget.message.read ? colorGrey : null,
              ),
            ),
            Text(
              DateFormat('MM/dd/yyyy - h:mm a').format(widget.message.date),
              style: TextStyle(
                fontSize: screenSize.width * 0.033,
                fontWeight: FontWeight.w600,
                color: colorGrey,
                height: 1.6,
              ),
            ),
          ],
        ),
      ],
    );

这是显示问题的图像:

enter image description here

我希望文本占据相同的空间,而不管大小写。 (对大写或小写进行简单检查不起作用,这会导致“在标题上存在类似THIS的文字” –小写字母和大写字母的数量不同)。

PS:等宽字体不起作用。

flutter flutter-layout
1个回答
0
投票

请勿应用子字符串或任何东西。

请尝试一个名为Text()overflow属性>

Text(
   "your long very long text",
   overflow: TextOverflow.ellipsis
)
© www.soinside.com 2019 - 2024. All rights reserved.