【问题标题】:How to wrap and scale down text at the same time in flutter?如何在颤动中同时换行和缩小文本?
【发布时间】:2019-09-19 07:38:57
【问题描述】:

我在圆形容器中有一个文本,该文本来自 API。所以我无法控制文本的长度。我通过使用 FittedBox 缩小它来显示容器内的文本,不管它有多长。

             Container(
                        height: 44,
                        width: 44,
                        padding: EdgeInsets.all(3),
                        alignment: Alignment.center,
                        child: Row(
                          children: [
                            Flexible(
                              child: FittedBox(
                                child: Text('Best Seller'),
                              ),
                            ),
                          ],
                        ),
                        decoration: BoxDecoration(shape: BoxShape.circle, color: Color(0xFFF37C43)),
                      )

我的问题是如何在新行中显示文本的所有单词,同时缩小它们。我正在考虑使用 Column 并从文本的每个单词生成子小部件。但这似乎不是很直观。如果需要,我愿意更改我的布局设计。

【问题讨论】:

  • 为什么投反对票?

标签: flutter dart flutter-layout


【解决方案1】:

使用自动大小文本。这是链接 https://pub.dev/packages/auto_size_text

【讨论】:

  • 感谢您的回答。我在调查时遇到了这个包裹。但我正在寻找一个内置的解决方案
【解决方案2】:
 Container(
        height: 44,
        width: 44,
        padding: EdgeInsets.all(3),
        alignment: Alignment.center,
        child: Wrap(
          children: [
            Text('Best Seller', style: TextStyle(fontSize: 10),),
          ],
        ),
        decoration:
            BoxDecoration(shape: BoxShape.circle, color: Color(0xFFF37C43)),
      ),

【讨论】:

  • 它只解决了我一半的问题,即换行。但是当它变大时,文本仍然会溢出容器。
【解决方案3】:

尝试设置与单词长度兼容的fontSize。示例:

Text(
  myString,
  style: TextStyle(
    fontSize: myString.length > 60? 30: myString.length > 40? 40 : 50,
  ),
)

【讨论】:

    猜你喜欢
    • 2020-09-10
    • 2020-08-03
    • 2020-02-06
    • 2022-07-06
    • 2018-12-06
    • 2017-02-08
    • 2020-05-24
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多