Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using AnimatedOpacity to hide and show complex widgets not effective in Flutter?

I'm using AnimatedOpacity to hide some widgets in Stack between various states in my app.

For instance I have Stack with ListView and animating loading indicator (shimmer) below, so that when list is populated the animated background opacity is gradually set to 0.

Stack:
 - AnimatedOpacity (
      opacity: _populated ? 0.0 : 1.0, 
      child: AnimatedLoadingWidget,
   ),
 - ListView

Do I understand correctly that the animation in AnimatedLoadingWidget continues and is rendered even if opacity is set to 0.0? Does it have a performance impact on the app?

like image 329
Dominik Roszkowski Avatar asked Aug 14 '19 10:08

Dominik Roszkowski


People also ask

How do you animate visibility in flutter?

Flutter provides AnimatedOpacity Widget to change the opacity of the widget when show and hide in progress. It will linearly change the opacity and you can change the curve (how the animation should happen) by setting values like bounceIn, easeInOut etc. The main property which you need to set is opacity.

How do you use the FadeTransition flutter?

FadeTransition allows you to blur a widget in and out by animating its opacity. In this way, you simply need to give the opacity boundary with animation and a child widget on which the animation must be performed.

How do you use tweens in flutter?

A linear interpolation between a beginning and ending value. Tween is useful if you want to interpolate across a range. To use a Tween object with an animation, call the Tween object's animate method and pass it the Animation object that you want to modify.

How to create animated opacity animation in flutter?

If you want to create an opacity animation on a widget when the opacity changes, it can be done easily using Flutter. A widget called AnimatedOpacity makes it easy to create such animation. To use the widget, you need to call the below constructor.

When to use visibility in animatedopacity?

Once that animation is complete and the child has an opacity of 0, is when to use Visibility. You can find the status of your animation through listeners, timers or any other method you can think of. You will find that Widgets like AnimatedOpacity also have callback functions.

What is animatedopacity?

Animated version of Opacity which automatically transitions the child's opacity over a given duration whenever the given opacity changes. AnimatedOpacity (Flutter Widget of the Week) Animating an opacity is relatively expensive because it requires painting the child into an intermediate buffer.

How to show or hide a widget in flutter?

In Flutter, it tends to be done effectively utilizing Visibility a widget. The widget you need to show or hide must be the child of Visibility widget. In the constructor, pass visibility alternative whose value is a boolean and is put away as the state. Then, at that point, update the value to show or hide the child.


1 Answers

If you're wondering whether or not the widget is still rendered when an opacity of 0, then no.

Opacity and its animated variant is clever enough to not render the child if the opacity is strictly equal to 0.

But that's not enough. Even with an opacity of 0, your loading widget is still in the widget tree, and its animation continues to play.

To be more specific, with typical:

Opacity(
  opacity: 0,
  child: const CircularProgressIndicator(),
)

the spinner will not be visible, but it will still endlessly request new frames.

To fix that, we can use TickerMode widget like so:

Opacity(
  opacity: 0,
  child: TickerMode(
    enabled: false,
    child: const CircularProgressIndicator(),
  ),
)

Doing so will "mute" animations played using the animation framework, and as such, the spinner will stop requesting for new frames.

Alternatively, you can use AnimatedCrossFade with a custom layoutBuilder to achieve a similar effect.

like image 94
Rémi Rousselet Avatar answered Nov 15 '22 09:11

Rémi Rousselet