Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Animation Text Fade transition

i wanna make a fade transition on two text , what i mean the value change from text to another by animation ... for example one .. two .. and repeat this value again and again

and that's what i tried to make

Container(
        alignment: Alignment.center,
        width: 150,
        height: 50,
        child: FadeTransition(
          opacity: controller2,
          child: Text('Breathe in ',textDirection: TextDirection.ltr,style: TextStyle(color: Colors.white,fontSize: 30,),),
        ),
        color: Colors.red,


 ),

How Can I Achieve This ?

like image 811
Mohamed M. Abo Elmagd Avatar asked Sep 03 '19 18:09

Mohamed M. Abo Elmagd


1 Answers

I think you can solve this using AnimatedOpacity, where it automatically animates, fading opacity in and out.

This sample code stacks 2 widgets, one red and one black, alternating which one have full opacity.

double opacity = 1.0;

@override
void initState() {
  super.initState();
  changeOpacity();
}

changeOpacity() {
  Future.delayed(Duration(seconds: 1), () {
    setState(() {
      opacity = opacity == 0.0 ? 1.0 : 0.0;
      changeOpacity();
    });
  });
}

Widget build(BuildContext context) {
  return Stack(
    children: <Widget>[
      AnimatedOpacity(
        opacity: opacity,
        duration: Duration(seconds: 1),
        child: Container(
          color: Colors.black,
        ),
      ),
      AnimatedOpacity(
        opacity: opacity == 1 ? 0 : 1,
        duration: Duration(seconds: 1),
        child: Container(
          color: Colors.red,
        ),
      ),
    ]
  );
}
like image 190
Evaldo Bratti Avatar answered Oct 22 '22 10:10

Evaldo Bratti