如何在 Flutter 中以编程方式显示/隐藏小部件 [英] How to show/hide widgets programmatically in Flutter

查看:15
本文介绍了如何在 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:

  • 可见:使 View 在布局内可见
  • 不可见:隐藏 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: 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 开发者关于 Opacity 的快速教程:https://youtu.be/9hltevOHQBw

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

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

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