Flutter:SizedBox与Container,为什么要使用其中一个而不是另一个?

Flutter:SizedBox与Container,为什么要使用其中一个而不是另一个?,第1张

Flutter:SizedBox与Container,为什么要使用其中一个而不是另一个?

多亏了开源的魔力,您不必猜测太多。

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
因为它更清晰。恕我直言。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5040161.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存