【问题标题】:How to fit Text to container without using FittedBox如何在不使用 FittedBox 的情况下将文本适合容器
【发布时间】:2022-08-04 17:55:27
【问题描述】:

使用 FittedBox 时,与文本长度相比,文本大小变得非常小。 我不希望文字缩小。相反,我希望文本保持相同的大小,并且只适合可以适合的部分。

Container(
 width: 250,
 child: Text(\"Data will end with ... instead of shrinking...\"),),

    标签: flutter dart


    【解决方案1】:

    为了使您的文本以这种方式工作,您应该设置文本在溢出时的行为方式。

    您需要做的是将overflow 属性设置为TextOverflow.ellipsis

    Text(
      'Hello, Test! How are you?',
      overflow: TextOverflow.ellipsis,
    )
    

    它在 Text 小部件的 Flutter 文档中提供:https://api.flutter.dev/flutter/widgets/Text-class.html

    另外,检查其他文本溢出选项:https://api.flutter.dev/flutter/painting/TextOverflow.html

    【讨论】:

    • 在我打开一个线程之前,我必须检查网站。我的错。谢谢您的帮助。
    【解决方案2】:

    您可以使用Flexible

    Container(
      child: Row(
      children: [
        Flexible(child: Text("A looooooooooooooooooong text"))
      ],
     ),
    ),
    

    有关Flexible 的更多信息,请查看 https://api.flutter.dev/flutter/widgets/Flexible-class.html

    【讨论】:

      【解决方案3】:
      FittedBox(
      fit:BoxFit.scaleDown,
      child: Text("hey, how are you",
      textAlign: TextAlign.start,
      maxLines: 1,
      style: TextStyle(
      fontWeight: FontWeight.normal,
      color: Colors.black.withOpacity(0.4))),
      )
      

      【讨论】:

        猜你喜欢
        • 2021-06-13
        • 2010-12-15
        • 1970-01-01
        • 2011-07-29
        • 2011-06-14
        • 1970-01-01
        • 2017-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多