flutter - 如何在 Flutter 中以编程方式显示/隐藏小部件

在 Android 中,每个 View 子类都有一个 setVisibility() 方法,允许您修改 View 对象的可见性

有 3 种设置可见性的选项:

  • 可见:使 View 在布局内可见
  • 不可见:隐藏 View,但留下的间隙相当于 View 可见时所占用的空间
  • Gone:隐藏 View,并将其从布局中完全移除。好像它的 heightwidth0dp

对于 Flutter 中的 Widgets 是否有与上述等价的东西?

快速引用: https://developer.android.com/reference/android/view/View.html#attr_android:visibility

最佳答案

定义:

不可见:小部件占用屏幕上的物理空间,但对用户不可见。这可以使用 Visibility 小部件来实现。

消失:小部件不占用任何物理空间,完全消失。这可以使用 Visibilityifif-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/

相关文章:

android - 如何使用 Flutter 更改应用显示名称构建?

dart - 具有透明背景的 flutter 角半径

flutter - 更改项目名称

git - flutter 安装期间的libusbmuxd版本错误

flutter - 在 Flutter 中延迟一段时间后如何运行代码?

dart - Flutter 移除所有路由

flutter - 如何在 flutter 中使用进度指示器?

flutter - 等待另一个flutter命令释放启动锁

flutter - 如何在 flutter 中为文本加下划线

dart - 如何将非字符串数据传递给 Flutter 中的命名路由?