颤动中的自动扩展容器——适用于所有设备

Auto expanding Container in flutter -- for all devices

我需要一个包含一些文本的容器来自动展开。我有一个 API 电话,可以是 5 个字到 500 个字之间的任何内容。我不想只有 1 个固定大小的巨大但包含 10 个单词。

我试过 Expanded() 和 SizedBox.Expand(),但我可能用错了

Card( 
 elevation: defaultTargetPlatform ==
      TargetPlatform.android ? 5.0 : 0.0,
  child: Column(
    children: <Widget>[
      Container(
        margin: const EdgeInsets.all(0.0),
        padding: const EdgeInsets.all(2.0),
        decoration: BoxDecoration(color: Colors.black),
        width: _screenSize.width,
        height: 250,
        child: Column(
          children: <Widget>[
            Container(
              color: Colors.black,
              width: _screenSize.width,
              height: 35,
              child: Padding(
                padding: const EdgeInsets.only(
                    left: 15, top: 11),
                child: Text("Title".toUpperCase(),
                  style: TextStyle(
                      color: Colors.white
                  ),
                ),
              ),
            ),
            Container(
              color: Colors.white,
              width: _screenSize.width,
              height: 210,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(top: 8, bottom: 5),
                    child: Text("Title of expanding text", style: TextStyle(
                      fontSize: 25,
                    ),
                    ),
                  ),
                  Text("Expanding text", style: TextStyle(
                      fontSize: 35,
                      fontWeight: FontWeight.w800
                  ),),
                ],
              ),
            ),
          ],
        ),
      ),
    ],
  ),
),

我只需要 Container 展开,但要保持 small/get 更大

您可以使用 FittedBox,这将根据可用区域调整文本大小。

你可以这样使用它:

FittedBox(child: Text('...............Your text...............'));

您是否尝试过完全不指定 height?在这种情况下,Container 应该根据 child 换行。

Otherwise, the widget has a child but no height, no width, no constraints, and no alignment, and the Container passes the constraints from the parent to the child and sizes itself to match the child.

以上是 Container.

官方 flutter 文档的摘录

这里是flutter官方文档link.

我建议您使用 Constraints...这将根据 Text 子项的要求设置 Container 高度。请看例子...

Container(
    constraints: BoxConstraints(
    maxHeight: double.infinity,
),
child: Column(
children: [
  Text(
    'Hello flutter...i like flutter...i like google...',
    softWrap: true,
    style: TextStyle(
        color: Colors.white, fontSize: 20 , ),

  ),],),)

我也有一个容器,里面有一个文本小部件,它不会随着文本字符数的增加而缩放。制作小部件树 Container -> IntrinsicWidth -> Text/TextField 它似乎对我来说很好玩。

IntrinsicWidth 会将容器的大小缩放到其子项。