【发布时间】: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'),
)
),
]
);
我尝试了几种涉及Expanded 和Flexible 的方法来解决它,但它们都会导致“红屏死机”(或至少是“烦恼”)。
编辑
明确一点 - 我希望 Column 仅包装其内容,即将其宽度调整为容器的宽度。总之 - 一切都应该尽可能窄,除了文本最短的容器,它应该与其兄弟(文本较长的容器)一样宽。
【问题讨论】:
-
扩展一个会导致越界问题,所以你必须用灵活标记另一个。但是,如果您想填充宽度,请尝试在容器上使用 Rows。像 Column -> (Row -> Container) , (Row -> Container) 之类的东西,然后随心所欲地使用所需的扩展进行播放。