【问题标题】:How to make a Container fit to the Text length如何使容器适合文本长度
【发布时间】:2021-07-26 01:31:32
【问题描述】:

我有一个最大宽度的容器,我不想在单词之后有额外的可用空间。它适用于像这样的小词 this 。 但是长话我有额外的可用空间,我的线条看起来 like that 。我能做什么?

这是我的代码:

Container(
                constraints: BoxConstraints(maxWidth: size.width * 0.4),
                padding: EdgeInsets.only(right: size.width * 0.015),
                //color: Colors.red,
                child: Text(
                  "Generics",
                  style:
                      TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                )),

【问题讨论】:

  • 如果没有完整的 Row 代码,有点难以提供帮助,但我会先看看 dart 调试工具并应用调试绘制。这可能会提供一些见解。

标签: flutter dart flutter-container


【解决方案1】:

尝试使用FittedBox。它将确保它的孩子拥有给定的fit

这是来自 flutter.dev 的示例:

Widget build(BuildContext) {
  return Container(
    height: 400,
    width: 300,
    color: Colors.red,
    child: FittedBox(
      child: Image.network('https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),
      fit: BoxFit.fill,
    ),
  );
}

阅读更多here

【讨论】:

  • 非常感谢您的回答!不幸的是,我尝试了这个,我得到了这个ibb.co/qsXjQMD也许你知道该怎么做?
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
相关资源
最近更新 更多