为容器提供固定的宽度和高度与通过 BoxConstraints class 提供约束

Giving a fixed width and height to a container vs providing constraints via a BoxConstraints class

在flutter的Container小部件中,

给它一个固定的 heightwidth 和为 constraints 属性 提供它 BoxConstraints 有什么区别?.

它是否提供 width + heightconstraints ?可以同时提供吗?有什么区别?

我还查看了 official docs,它指的是 Container 小部件,在那里,就在名为 Layout Behavior 的标题下,他们提到了发生了什么当不同的宽高组合和约束条件呢? (但我不明白他们有什么不同)

TLDR:他们做同样的事情

长答案: 如果您查看 Container 的代码,您会发现您提供的 widthheight 实际上变成了 BoxConstraintsContainer 将使用 BoxConstraints 来设置其约束。这是 Container 构造函数的 2021 年 10 月片段:

  Container({
    Key? key,
    this.alignment,
    this.padding,
    this.color,
    this.decoration,
    this.foregroundDecoration,
    double? width,
    double? height,
    BoxConstraints? constraints,
    this.margin,
    this.transform,
    this.transformAlignment,
    this.child,
    this.clipBehavior = Clip.none,
  }) : // *Removed the asserts it made to make this snippet shorter
       // Important part is here
       constraints =
        (width != null || height != null)
          ? constraints?.tighten(width: width, height: height)
            ?? BoxConstraints.tightFor(width: width, height: height)
          : constraints,
       super(key: key);

这是 Container 构建方法的一部分:

// Parts of the code removed to shorten snippet
if (constraints != null)
  current = ConstrainedBox(constraints: constraints!, child: current);
// Parts of the code removed to shorten snippet
return current!;