惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
罗磊的独立博客
U
Unit 42
I
InfoQ
B
Blog RSS Feed
Google DeepMind News
Google DeepMind News
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog
S
SegmentFault 最新的问题
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园_首页
腾讯CDC
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
D
Docker
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
V
V2EX
Last Week in AI
Last Week in AI
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
L
LangChain Blog
WordPress大学
WordPress大学
Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享

开源实验室

一个月,纯VibeCoding,全平台云笔记APP|开源实验室 kymjs.com 借助 API 手写一个 Transformer 架构|开源实验室 PT 站规则扫盲|开源实验室 使用 RAGFlow 搭建一套 AI 客服知识库|开源实验室 终于搞定了,本地部署 RAGFlow|开源实验室 阿富汗-中亚最倒霉国家|开源实验室 【HarmonyOS】TheRouter 鸿蒙版新手入门|开源实验室 可能是 AGP8 编译最快的方案|开源实验室 NAS - 玩点有意思的|开源实验室 动态路由 TheRouter 的设计与实践|开源实验室 没错,TheRouter 是我写的|开源实验室 货拉拉 Android 模块化路由框架:TheRouter|开源实验室 菲律宾这国家到底怎么回事|开源实验室 印度的穆斯林王朝——德里苏丹|开源实验室 学历史有什么用|开源实验室 印度的列国时代大一统——孔雀王朝|开源实验室 印度列国时代的百家争鸣——沙门思潮|开源实验室 印度真是个神奇的国度——总章|开源实验室 某一天,中国真会超过美国吗?|开源实验室 基于 C++ 的 Android 协程设计|开源实验室 Gradle 6.X 上传 aar 到 Nexus 私服|开源实验室 自定义C/C++日志输出函数|开源实验室 扁平化管理,就是管理者的失职|开源实验室 再聊 Git Flow|开源实验室 Flutter 线性布局:Column 和 Row|开源实验室 开源一段 Mac 批量压缩图片的脚本|开源实验室 Leader 的自我修养——学会预测|开源实验室 玩玩区块链——概念|开源实验室 一条电商 Android 工程化实践|开源实验室
Flutter 设置控件是否可见|开源实验室
2020-03-19 · via 开源实验室
对本知识点有任何问题,可加我的个人微信:kymjs666

共有两种实现比较简单的方式

第一种比较好理解,将一个控件的透明度设置成0,达到隐藏的目的。

class _HideAndShowPageState extends State<HideAndShowPage> {
bool visible = true;

@override
Widget build(BuildContext context) {
  return new Scaffold(
    appBar: new AppBar(
      title: new Text('widget显示与隐藏'),
      centerTitle: true,
    ),
    body: new ListView(
      children: <Widget>[
        new Padding(
          padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
          child: new RaisedButton(
              textColor: Colors.black,
              child: new Text(visible ? '隐藏B    显示A' : '隐藏A   显示B'),
              onPressed: () {
                visible = !visible;
                setState(() {});
              }),
        ),
        new Padding(
          padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
          child: new Stack(
            children: <Widget>[
              new TestAWidget(
                visible: visible,
              ),
              new TestBWidget(
                visible: !visible,
              ),
            ],
          ),
        ),
      ],
    ),
  );
}
}

class TestAWidget extends StatelessWidget {
final bool visible;

const TestAWidget({Key key, this.visible}) : super(key: key);

@override
Widget build(BuildContext context) {
  return AnimatedOpacity(
    duration: Duration(milliseconds: 300),
    opacity: visible ? 1.0 : 0.0,
    child: new Container(
      color: Colors.blue,
      height: 100.0,
      child: new Center(
        child: new Text('TestAWidget'),
      ),
    ),
  );
}
}

class TestBWidget extends StatelessWidget {
final bool visible;

const TestBWidget({Key key, this.visible}) : super(key: key);

@override
Widget build(BuildContext context) {
  return AnimatedOpacity(
    duration: Duration(milliseconds: 300),
    opacity: visible ? 1.0 : 0.0,
    child: new Container(
      color: Colors.green,
      height: 100.0,
      child: new Center(
        child: new Text('TestBWidget'),
      ),
    ),
  );
}
}

第二种办法是使用 SDK 自带的 Offstage 控件包裹。

offstage的布局行为完全取决于 offstate 参数,offstage 默认为 true ,不显示;

当 offstage 为 true,child 不会绘制到屏幕上,不会响应点击事件,也不会占用空间; 当 offstage 为 false,child 绘制到屏幕上; 注意,当 offstage 不可见,如果 child 有动画,应该手动停止动画, offstage 不会停止动画;

class TestCWidget extends StatelessWidget {
 final bool visible;

 const TestCWidget({Key key, this.visible}) : super(key: key);

 @override
 Widget build(BuildContext context) {
   return new Offstage(
     offstage: visible,
     child:new Container(
       color: Colors.orange,
       height: 100.0,
       child: new Center(
         child: new Text('TestCWidget'),
       ),
     ),
   );
 }
}