【问题标题】:Make container grow to fit width of sibling in column使容器增长到适合列中同级的宽度
【发布时间】:2019-06-17 19:30:19
【问题描述】:

我在Column 中有两个Container 小部件,每个都包含一个Text 小部件。我希望具有最短文本的 Container 扩展以匹配包含较长文本的另一个容器的宽度。文本较长的容器应包裹 Text 小部件及其填充。

如果不使用容器上的固定宽度,我无法让它工作:

return Column(
    mainAxisSize: MainAxisSize.min,
    children: [
        // This container has a shorter text than its sibling, so it should expand 
        // its width to match its sibling/parent
        Container(
            alignment: Alignment.center,
            width: 50, // I don't want this fixed width
            decoration: BoxDecoration(color: Colors.orange, borderRadius: BorderRadius.only(topLeft: Radius.circular(4), topRight: Radius.circular(4))),
            child: Padding(
                padding: const EdgeInsets.fromLTRB(8,4,8,4),
                child: Text('short'),
            )
        ),

        // This container has a longer text, so it should decide the width of the 
        // other container (and of the parent Column)
        Container(
            alignment: Alignment.center,
            width: 50, // I don't want this fixed width
            decoration: BoxDecoration(color: Colors.orange, borderRadius: BorderRadius.only(bottomLeft: Radius.circular(4), bottomRight: Radius.circular(4))),
            child: Padding(
                padding: const EdgeInsets.fromLTRB(8,4,8,4),
                child: Text('long text here'),
            )
        ),
    ]
);

我尝试了几种涉及ExpandedFlexible 的方法来解决它,但它们都会导致“红屏死机”(或至少是“烦恼”)。

编辑

明确一点 - 我希望 Column 仅包装其内容,即将其宽度调整为容器的宽度。总之 - 一切都应该尽可能窄,除了文本最短的容器,它应该与其兄弟(文本较长的容器)一样宽。

【问题讨论】:

  • 扩展一个会导致越界问题,所以你必须用灵活标记另一个。但是,如果您想填充宽度,请尝试在容器上使用 Rows。像 Column -> (Row -> Container) , (Row -> Container) 之类的东西,然后随心所欲地使用所需的扩展进行播放。

标签: flutter flutter-layout


【解决方案1】:

我在评论中看到了你的小提琴。你需要的是IntrinsicWidth

  IntrinsicWidth(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        // This container has a shorter text than its sibling, so it should expand
        // its width to match its sibling/parent
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 1.0),
          child: Container(
            color: Colors.orange,
            child: Padding(
              padding: const EdgeInsets.fromLTRB(8, 4, 8, 4),
              child: Text(
                'short',
                textAlign: TextAlign.center,
              ),
            ),
          ),
        ),

        // This container has a longer text, so it should decide the width of the
        // other container (and of the parent Column)
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 1.0),
          child: Container(
            color: Colors.orange,
            child: Padding(
              padding: const EdgeInsets.fromLTRB(8, 4, 8, 4),
              child: Text(
                'long text here',
                textAlign: TextAlign.center,
              ),
            ),
          ),
        ),
      ],
    ),
  ),

【讨论】:

  • @MagnusW 我已经根据你的小提琴更新了我的答案。
  • 谢谢,它正在工作!我也尝试在另一种情况下使用此解决方案(ColumnFlatButton,但在这种情况下,我必须将此答案与@Mariano Zorrilla 的答案结合起来才能使其工作,即IntrinsicWidth 周围的 Wrap 小部件。
  • crossAxisAlignment: CrossAxisAlignment.stretch,也是解决方案的关键部分
【解决方案2】:

您可以使用类似这样的方法来获取 Widget 的整个宽度(确保位于 MaterialApp 和 Scaffold/Material 子级(home)中:

Wrap(
   children: <Widget>[
      Container(
           color: Colors.red,
           child: IntrinsicWidth(
                child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                   Container(
                     color: Colors.black,
                     height: 200,
                     child: Align(
                       alignment: Alignment.bottomRight,
                       child: Text(
                            "Hello World",
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                      ),
                   Container(
                     color: Colors.blue,
                     height: 100,
                     child: Align(
                     alignment: Alignment.topRight,
                          child: Text(
                            "Bottom Text Longer Text",
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          )

对齐不是必需的,只是为了展示宽度实际上如何与“double.infinity”一起使用并将文本移动到它的右下角或右上角。

【讨论】:

  • 我无法让它工作 - 我猜这需要Column 的父级具有固定宽度?我希望Column 包装容器和包装文本的容器,但容器的文本宽度最小,以匹配父项的宽度(即Column)。
  • 您是否尝试使用 MaterialApp 和 Scaffold 作为房屋价值?您可以将我的逻辑用于脚手架的主体。我刚刚测试过,它可以工作。 (或者你可以使用 Material Widget 而不是 Scaffold)。
  • 我试过了,但是 Column 的宽度占据了其父级的整个宽度,这正是我想要避免的 - 我希望它 wrap容器,即缩小以适应。请参阅 this jsfiddle 了解我的意思。
  • @MagnusW 检查我的更新回复。现在这就是你所需要的。您甚至可以删除具有红色背景颜色的容器(仅用于测试)。
猜你喜欢
  • 1970-01-01
  • 2017-08-08
  • 2022-12-01
  • 2018-02-28
  • 2023-03-08
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 2020-11-19
相关资源
最近更新 更多