flutterfadeinflutter-animationfadeout

Flutter FadeIn/FadeOut animation together


in this simple sample code i want to have fadeIn and fadeOut animation together, but in this code fadeIn work only and reverse not work, how can i have both of them together?

import 'package:flutter/material.dart';

void main()=>runApp(MaterialApp(home: FadeTransitionSample(),));
class FadeTransitionSample extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _Fade();
}

class _Fade extends State<FadeTransitionSample> with TickerProviderStateMixin {
  AnimationController animation;
  Animation<double> _fadeInFadeOut;

  @override
  void initState() {
    super.initState();
    animation = AnimationController(vsync: this, duration: Duration(seconds: 3),);
    _fadeInFadeOut = Tween<double>(begin: 0.0, end: 0.1).animate(animation);

    animation.addListener((){
      if(animation.isCompleted){
        animation.reverse();
      }else{
        animation.forward();
      }
    });
    animation.repeat();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: FadeTransition(
            opacity: animation,
            child: Container(
              color: Colors.green,
              width: 100,
              height: 100,
            ),
          ),
        ),
      ),
    );
  }
}

Solution

  • Alright, I am assuming, you are looking to get a FadeIn & FadeOut animation on your container.

    There are a few things you need to change.

    1. The FadeTransition class should not take the animation for opacity, rather it should be the _fadeInFadeOut variable
    2. The animation starts, when you call the animation.forward() rather than animation.repeat() (Since in your case, you also need to reverse the animation, always start with the animation.forward() call).

    Make sure to use the addStatusListener() method instead of addListener() since you get much better control over your states.

    So, all of this put together, below is the working code to make your animation work.

    import 'package:flutter/material.dart';
    
    void main()=>runApp(MaterialApp(home: FadeTransitionSample(),));
    class FadeTransitionSample extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => _Fade();
    }
    
    class _Fade extends State<FadeTransitionSample> with TickerProviderStateMixin {
      AnimationController animation;
      Animation<double> _fadeInFadeOut;
    
      @override
      void initState() {
        super.initState();
        animation = AnimationController(vsync: this, duration: Duration(seconds: 3),);
        _fadeInFadeOut = Tween<double>(begin: 0.0, end: 0.5).animate(animation);
    
        animation.addStatusListener((status){
          if(status == AnimationStatus.completed){
            animation.reverse();
          }
          else if(status == AnimationStatus.dismissed){
            animation.forward();
          }
        });
        animation.forward();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            child: Center(
              child: FadeTransition(
                opacity: _fadeInFadeOut,
                child: Container(
                  color: Colors.green,
                  width: 100,
                  height: 100,
                ),
              ),
            ),
          ),
        );
      }
    }