Flutter animation how to fade in/out gradually. I think you’re on the right track, but you should only use one AnimationController per AnimatedWidget. I fixed some bugs in your code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import 'package:flutter/material.dart'; import 'dart:math' as math; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( home: new MyHomePage(), ); } } class MyHomePage extends StatelessWidget { Widget build(BuildContext context) { return new Scaffold( body: new AppLoader(), ); } } class PulsateCurve extends Curve { @override double transform(double t) { if (t == 0 || t == 1) return 0.3; return math.sin(t * math.PI) * 0.35 + 0.65; } } class AnimatedLoader extends AnimatedWidget { static final _opacityTween = new CurveTween(curve: new PulsateCurve()); AnimatedLoader({ Key key, this.alignment: FractionalOffset.center, Animation<double> animation, this.child, }) : super(key: key, listenable: animation); final FractionalOffset alignment; final Widget child; @override Widget build(BuildContext context) { final Animation<double> animation = listenable; final Matrix4 transform = new Matrix4.rotationZ(animation.value * math.PI * 2.0); return new Transform( alignment: alignment, transform: transform, child: new Opacity( opacity: _opacityTween.evaluate(animation), child: child, ) ); } } class AppLoader extends StatefulWidget { AppLoaderState createState() => new AppLoaderState(); } class AppLoaderState extends State<AppLoader> with TickerProviderStateMixin { AnimationController _controller; @override initState() { super.initState(); _controller = new AnimationController( duration: const Duration(milliseconds: 1500), vsync: this, )..repeat(); } @override Widget build(BuildContext context) { return new Center ( child: new AnimatedLoader( animation: _controller, alignment: FractionalOffset.center, child: new Container( margin: new EdgeInsets.symmetric(vertical: 10.0), height: 150.0, width: 150.0, child: new FlutterLogo(), ) ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.