Flutter Container()与SizedBox()的虚拟清空 [英] Flutter Container() vs SizedBox() for dummy empty

查看:66
本文介绍了Flutter Container()与SizedBox()的虚拟清空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很好奇。我已经看到很多使用 Container()作为虚拟隐藏小部件的示例,例如,加载完成后,我们 setState((){_isLoaded = true;});
那么我们可以使用这样的状态,对吗?

I am curious about this. I have seen many example using Container() for dummy hidden widget, for example, when loading has completed, then we setState(() { _isLoaded = true; });. So we can use the state like this, right?

return _isLoaded ? Container() : LoaderWidget();

或者实际上使用 SizedBox()更好,因为它不需要太多参数并且经常用于填充?

Or maybe using SizedBox() is actually better because it don't take much parameters and often used for padding anyway?

return _isLoaded ? SizedBox() : LoaderWidget();

我错了吗?

推荐答案

如果用作占位符:

容器如果小部件没有子级,没有高度,没有宽度,没有约束,也没有对齐,但是父级提供了有限的约束,然后Container展开以适合父级提供的约束。

Container if the widget has no child, no height, no width, no constraints, and no alignment, but the parent provides bounded constraints, then Container expands to fit the constraints provided by the parent.

SizedBox 如果小部件没有子级,没有高度,没有宽度,则宽度和高度为零。

SizedBox if the widget has no child, no height, no width, then width and height are zero.

因此, SizedBox()更多用作语法占位符。

Therefore, SizedBox() serves more as a syntactic placeholder.

此外,请注意, SizedBox() const 构造函数,但 Container()不是。第一个允许编译器创建更有效的代码。

Also, note that SizedBox() is a const constructor but Container() is not. The first one allows the compiler to create more efficient code.

这篇关于Flutter Container()与SizedBox()的虚拟清空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆