以编程方式在 Flutter 中显示/隐藏小部件 [英] Show/hide widgets in Flutter programmatically

查看:32
本文介绍了以编程方式在 Flutter 中显示/隐藏小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Android, every single View subclass has a setVisibility() method that allows you modify the visibility of a View object

设置可见性有 3 个选项:

There are 3 options of setting the visibility:

  • Visible:在布局内呈现View可见
  • Invisible:隐藏 View,但留下一个间隙,相当于 View 如果可见
  • Gone:隐藏View,并将其从布局中完全移除.就好像它的 heightwidth0dp
  • Visible: Renders the View visible inside the layout
  • Invisible: Hides the View, but leaves a gap that is equivalent to what the View would occupy if it were visible
  • Gone: Hides the View, and removes it entirely from the layout. It's as if its height and width were 0dp

对于 Flutter 中的 Widgets 是否有与上述相同的东西?

Is there something equivalent to the above for Widgets in Flutter?

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

推荐答案

UPDATE:自从写了这个答案,Visibility 被引入并为这个问题提供了最好的解决方案.

UPDATE: Since this answer was written, Visibility was introduced and provides the best solution to this problem.

您可以使用 Opacityopacity:0.0 来绘制使元素隐藏但仍占用空间.

You can use Opacity with an opacity: of 0.0 to draw make an element hidden but still occupy space.

为了不占用空间,用一个空的Container()替换它.

To make it not occupy space, replace it with an empty Container().

要将其包装在 Opacity 对象中,请执行以下操作:

To wrap it in an Opacity object, do the following:

            new Opacity(opacity: 0.0, child: new Padding(
              padding: const EdgeInsets.only(
                left: 16.0,
              ),
              child: new Icon(pencil, color: CupertinoColors.activeBlue),
            ))

关于不透明度的 Google Developers 快速教程:https://youtu.be/9hltevOHQBw

Google Developers quick tutorial on Opacity: https://youtu.be/9hltevOHQBw

这篇关于以编程方式在 Flutter 中显示/隐藏小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆