iTranslated by AI
Toggling Visibility in Flutter: Using Visibility and AnimatedOpacity Widgets
TL;DR
- If you simply want to control show/hide, there is
Visibility.- Visibility class | Flutter
- You can also control whether to reserve or release the space occupied by the object when it is hidden.
- If you want to fade in/out during show/hide transitions, use
AnimatedOpacity.- AnimatedOpacity class | Flutter
- For more complex control, use
FadeTransition. - However, since this only affects opacity, you need separate control if you want to manage the hidden area.
- That said, if you want to remove the space after fading out, you would likely need an animation that collapses the area.
Visibility Didn't Exist in the Past
It seems that the Visibility class was not provided in the early days.
Searching on Google brings up articles about implementing it yourself.
It appears to have become available around June 2019.
- Managing visibility in Flutter | Medium
- Creating a Visibility widget in Flutter | Qiita
- Japanese explanation of the article above
- https://qiita.com/nichiyoshi/items/90ddb18fe8fd5123dd5e
- [Flutter] Switching Widget visibility dynamically | Qiita
- Technique of replacing with an empty
Container - https://qiita.com/rkowase/items/c4883a6ebc91e3f4a345
- Technique of replacing with an empty
- Show/hide widgets in Flutter programmatically | stackoverflow
- There is a discussion here about how Visibility has finally been introduced
- https://stackoverflow.com/questions/44489804/show-hide-widgets-in-flutter-programmatically
How to Use Visibility
It is very easy to use if you just want to simply toggle visibility.
To remove the space when hidden
// _visible = false; to hide
Visibility(
visible: _visible,
child: MyWidget(),
)
To keep reserving the space even when hidden
// _visible = false; to hide
Visibility(
visible: _visible,
maintainSize: true,
child: MyWidget(),
)
Other properties are also available:
-
maintainState -
maintainAnimation -
maintainInteractivity -
replacement -
Widget Hide and Seek: A guide to managing Flutter widget’s visibility | Medium
-
Visibility class | Flutter
- https://api.flutter.dev/flutter/widgets/Visibility-class.html
How to Use AnimatedOpacity
Use this if you want to fade in/out when showing/hiding.
Specify the transparency with opacity and the fade time with duration.
Since it only affects opacity, the space will be reserved after making it transparent.
// _visible = false; to hide
AnimatedOpacity(
opacity: _visible ? 0.0 : 1.0,
duration: Duration(milliseconds: 200),
child: MyWidget(),
)
Since it only affects opacity, it would look unnatural to remove the space after making it transparent without an animation that collapses it.
It might be possible by using AnimatedSwitcher, wrapping it in an AnimatedContainer, and setting width or height to 0 according to _visible?
I haven't tried it yet, though.
→ Updated 2021/11/24
I received a comment informing me that this can be done!
Discussion
IgnorePointerを使うと簡単にAnimatedOpacityの領域も消せましたので他の方の参考にもなれば:)@wake さん 情報追記ありがとうございます!
もう結論出てると思いますが、一応確認したところ出来ました。
ご報告までに。
@蓮美 さん
ありがとうございます!追記しておきました🙇♂️