Flutter控制组件显示和隐藏三种方式详解
作者:莲花小怪兽 时间:2023-07-07 03:03:30
方式一:if语句控制
// 例如:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if(a=="显示")
Text("显示"),
Offstage(
offstage: false,
child: Text("显示"),
),
Visibility(
visible: true,
child: Text("显示")
)
],
),
方式二:Offstage组件
offstage属性控制控制child的显示和隐藏,true时:隐藏,false时:显示
Offstage(
offstage: true,
child: Text("显示"),
)
方式三: Visibility
visible属性控制child的显示和隐藏,true时:显示,false时:隐藏
Visibility(
visible: true,
child: Text("显示")
)
Offstage和Visibility的区别:
1、Visibility 隐藏/可见,能保存组件的状态;Offstage不能保存组件的状态,组件重新加载。
2、当Offstage不可见(true)的时候,如果child有动画等,需要手动停掉,Offstage并不会停掉动画等操作。
3、Offstage隐藏时不占空间,Visibility可以设置隐藏时占据空间也可以不占据空间,默认不隐藏时不占据空间
备注:Visibility隐藏时占据空间如下:
Visibility(
visible:false,
maintainAnimation: true,
maintainSize: true,
maintainState: true,
child: Text("显示"),
),
强调:maintainSize就是保持大小不变,如果只设置这个属性,会报错,另外两个属性:maintainAnimation和maintainState也必须同时设置。
来源:https://www.jianshu.com/p/8e40e281c469
标签:Flutter,控制组件,组件显示,组件隐藏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#邮件定时群发工具Atilia用法实例
2022-06-13 11:50:24
![](https://img.aspxhome.com/file/2023/1/106351_0s.png)
c# Struct的一些问题分析
2023-08-31 08:27:36
Android ExpandableListView单选以及多选实现代码
2023-01-14 20:45:51
![](https://img.aspxhome.com/file/2023/9/123269_0s.jpg)
java实现图片滑动验证(包含前端代码)
2022-03-21 12:13:52
![](https://img.aspxhome.com/file/2023/8/60538_0s.jpg)
c# 反射用法及效率对比
2022-06-11 17:50:19
![](https://img.aspxhome.com/file/2023/2/88322_0s.png)
浅谈C#在网络波动时防重复提交的方法
2022-07-23 22:37:01
![](https://img.aspxhome.com/file/2023/3/92913_0s.png)
SpringBoot 如何添加容器启动的初始化逻辑的操作方法
2021-11-20 12:10:39
![](https://img.aspxhome.com/file/2023/2/79272_0s.png)
Android 开发中layout下的子文件夹
2021-10-03 16:10:44
![](https://img.aspxhome.com/file/2023/7/128957_0s.png)
Java泛型中<?>和<T>的区别浅析
2023-11-25 05:20:09
SpringBoot整合RedisTemplate实现缓存信息监控的步骤
2023-10-14 10:17:13
Android 获取 usb 权限的两种方法
2022-01-06 14:37:06
![](https://img.aspxhome.com/file/2023/0/94240_0s.png)
使用SpringBoot配置https(SSL证书)
2022-04-03 19:37:00
![](https://img.aspxhome.com/file/2023/3/108423_0s.png)
android图像绘制(一)多种方法做图像镜像
2023-08-23 01:34:18
深入探究Java线程的创建与构造方法
2023-05-29 22:02:45
![](https://img.aspxhome.com/file/2023/5/72315_0s.png)
举例讲解设计模式中的访问者模式在Java编程中的运用
2022-04-04 08:00:45
![](https://img.aspxhome.com/file/2023/1/113401_0s.png)
java可变参数当做数组处理的方法示例
2023-07-03 13:48:21
Java设计模式之工厂模式分析【简单工厂、工厂方法、抽象工厂】
2021-07-07 21:11:57
C#实现将汉字转化为2位大写的16进制Unicode的方法
2022-03-11 21:45:07
mybatis关系映射之一对多和多对一
2021-08-01 21:14:26
![](https://img.aspxhome.com/file/2023/3/67973_0s.png)
SpringBoot 如何实时刷新静态文件
2023-11-02 13:27:09
![](https://img.aspxhome.com/file/2023/5/58765_0s.png)