将受限框的大小调整为其 maxHeight resp。最小高度

标签 flutter flutter-layout

我有一个 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/

相关文章:

android - 在flutter中使用setchanged方法时如何禁用ui build

flutter - 如何在 Flutter 中将我的应用程序设置为横向模式?

android - 修复 Flutter 中的 Unicode 渲染

dart - Flutter 下省 Dart 版

flutter - 如何检测用户是否开始垂直滚动 Listview.builder?

当指针到达边缘时 flutter 滚动屏幕

google-maps - flutter 谷歌地图: update markers

flutter - 带指针指示器的定制水平 slider

flutter - 如何检测我的 Flutter 应用程序是否在网络中运行?

flutter :IconButton onPressed 不适用于 SimpleDialog