我有一个 minHeight 为 50 和 maxHeight 为 400 的 ConstrainedBox。CB 包含一个将自身着色为黄色的 Container。
请注意,这里的 CB 是一种抽象。基本上,该 CB 可能是具有最小/最大约束的任何类型的小部件。
现在我有两种情况:
我想让 CB 尽可能大(取 maxHeight)。
我想让 CB 尽可能小(取 minHeight)。
我试过了
Widget build(BuildContext _) => Scaffold(
backgroundColor: Colors.transparent,
body: SizedBox.expand(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 50, maxHeight: 400),
child: Container(color: Colors.yellow))));
显示全屏黄色框,但我想要 400 像素!
Widget build(BuildContext _) => Scaffold(
backgroundColor: Colors.transparent,
body: SizedBox.shrink(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 50, maxHeight: 400),
child: Container(color: Colors.yellow))));
结果是空的脚手架,但我想要 50 像素!
编辑:我解决了这个问题,请看我自己的答案。
最佳答案
经过一番摸索,我发现了以下几点:
Widget build(BuildContext _) => Scaffold(
backgroundColor: Colors.transparent,
body: IntrinsicHeight(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 50, maxHeight: 400),
child: Container(color: Colors.yellow))));
将以 50 像素渲染框,并且
Widget build(BuildContext _) => Scaffold(
backgroundColor: Colors.transparent,
body: Container(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 50, maxHeight: 400),
child: Container(color: Colors.yellow))));
将以 400 像素渲染框。
因此,看起来 IntrinsicHeight 会将子项的大小调整为其 minHeight
,而仅将其放入 Container 中会产生 maxHeight
。
关于将受限框的大小调整为其 maxHeight resp。最小高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56409157/