网上查了下隐藏控件的主要有两种:

1.通过透明度

2.通过Offstage

但这两种方式隐藏的控件都会被加载进来,如果是自定义widget,都会调用initState方法。

如果想显示的时候才加载,隐藏的时候就移除的话,也有两种方式:

1.通过方法返回值

buildTestWidget() {
    if (xxx) {
        // 真正需要展示的空间
        return Widget();    
    } else {
        // 空白的占位符,不能返回null
        return Container(
            width: 0,
            height: 0
        );
    }
}

2.通过返回空白占位符,应该大部分人都能想到,这种方式不优雅。最佳方法如下:

Visibility(
    visible: 是否显示,
    child: Widget(),
)

文章转自:https://blog.csdn.net/xizhao88/article/details/89215875

相关文章:

  • 2021-12-13
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2022-03-05
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
  • 2022-02-10
  • 2022-12-23
相关资源
相似解决方案