多亏了开源的魔力,您不必猜测太多。
Container基本上只是一个便利小部件,有时可以使您节省嵌套其他4个小部件的时间。如果您将宽度/高度传递给
Container:
constraints = (width != null || height != null) ? constraints?.tighten(width: width, height: height) ?? BoxConstraints.tightFor(width: width, height: height) : constraints,
这将导致:
if (constraints != null) current = ConstrainedBox(constraints: constraints, child: current);
实际上,ConstrainedBox与a几乎相同
SizedBox,只是更加灵活。
A
SizedBox会做:
@override RenderConstrainedBox createRenderObject(BuildContext context) { return RenderConstrainedBox( additionalConstraints: _additionalConstraints, ); } BoxConstraints get _additionalConstraints { return BoxConstraints.tightFor(width: width, height: height); }
即。实际上是一样的。如果仅
Container用于宽度/高度,则性能开销可能很小,可以忽略不计。但您肯定会无法测量。.但我仍然建议您这样做,
SizedBox因为它更清晰。恕我直言。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)