以编程方式在 Flutter 中显示/隐藏小部件 [英] Show/hide widgets in Flutter programmatically
问题描述
在Android中,每个View
子类都有一个setVisibility()
方法,允许你修改View
对象的可见性
In Android, every single View
subclass has a setVisibility()
method that allows you modify the visibility of a View
object
设置可见性有 3 个选项:
There are 3 options of setting the visibility:
- Visible:在布局内呈现
View
可见 - Invisible:隐藏
View
,但留下一个间隙,相当于View
如果可见 - Gone:隐藏
View
,并将其从布局中完全移除.就好像它的height
和width
是0dp
- Visible: Renders the
View
visible inside the layout - Invisible: Hides the
View
, but leaves a gap that is equivalent to what theView
would occupy if it were visible - Gone: Hides the
View
, and removes it entirely from the layout. It's as if itsheight
andwidth
were0dp
对于 Flutter 中的 Widgets 是否有与上述相同的东西?
Is there something equivalent to the above for Widgets in Flutter?
快速参考:https://developer.android.com/reference/android/view/View.html#attr_android:visibility
推荐答案
UPDATE:自从写了这个答案,Visibility
被引入并为这个问题提供了最好的解决方案.
UPDATE: Since this answer was written, Visibility
was introduced and provides the best solution to this problem.
您可以使用 Opacity
和 opacity:
的 0.0
来绘制使元素隐藏但仍占用空间.
You can use Opacity
with an opacity:
of 0.0
to draw make an element hidden but still occupy space.
为了不占用空间,用一个空的Container()
替换它.
To make it not occupy space, replace it with an empty Container()
.
要将其包装在 Opacity 对象中,请执行以下操作:
To wrap it in an Opacity object, do the following:
new Opacity(opacity: 0.0, child: new Padding(
padding: const EdgeInsets.only(
left: 16.0,
),
child: new Icon(pencil, color: CupertinoColors.activeBlue),
))
关于不透明度的 Google Developers 快速教程:https://youtu.be/9hltevOHQBw
Google Developers quick tutorial on Opacity: https://youtu.be/9hltevOHQBw
这篇关于以编程方式在 Flutter 中显示/隐藏小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!