在 Android 中,每个 View
子类都有一个 setVisibility()
方法,允许您修改 View
对象的可见性
有 3 种设置可见性的选项:
View
在布局内可见View
,但留下的间隙相当于 View
可见时所占用的空间View
,并将其从布局中完全移除。好像它的 height
和 width
是 0dp
对于 Flutter 中的 Widgets 是否有与上述等价的东西?
快速引用: https://developer.android.com/reference/android/view/View.html#attr_android:visibility
最佳答案
不可见:小部件占用屏幕上的物理空间,但对用户不可见。这可以使用 Visibility
小部件来实现。
消失:小部件不占用任何物理空间,完全消失。这可以使用 Visibility
、if
或 if-else
条件来实现。
Visibility(
child: Text("Invisible"),
maintainSize: true,
maintainAnimation: true,
maintainState: true,
visible: false,
),
Visibility(
child: Text("Gone"),
visible: false,
),
if
:对于一个 child :
Column(
children: <Widget>[
Text('Good Morning'), // Always visible
if (wishOnePerson) Text(' Mr ABC'), // Only visible if condition is true
],
)
对于多个 child :
Column(
children: [
Text('Good Morning'), // Always visible
if (wishAll) ... [ // These children are only visible if condition is true
Text('Mr ABC'),
Text('Mr DEF'),
Text('Mr XYZ'),
],
],
)
if-else
:对于一个 child :
Column(
children: <Widget>[
// Only one of them is visible based on 'isMorning' condition
if (isMorning) Text('Good Morning')
else Text ('Good Evening'),
],
)
对于多个 child :
Column(
children: [
// Only one of the children will be shown based on `beforeSunset` condition
if (beforeSunset) ... [
Text('Good morning'),
Text('Good afternoon'),
] else ... [
Text('Good evening'),
Text('Good night'),
],
],
)
https://stackoverflow.com/questions/44489804/