Problems with multiple tween animations in Flutter – Videos Included. I managed to sort this using SlideTransition and FadeTransition. I guess we should only use Transition widgets for… transitions? while things like Positioned and Opacity are for more static widgets? Not sure…
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 | class WeatherCloudWidget extends StatefulWidget { final double sunSize; final CloudProperties properties; WeatherCloudWidget({Key key, this.properties, this.sunSize}) : super(key: key); @override State<StatefulWidget> createState() => _WeatherCloudWidget(); } class _WeatherCloudWidget extends State<WeatherCloudWidget> with TickerProviderStateMixin { AnimationController controller; Animation<Offset> position; Animation<double> opacity; final alphaTween = new Tween(begin: 0.0, end: 1.0); @override initState() { super.initState(); _startAnimation(); } @override Widget build(BuildContext context) { // screen width and height final screenWidth = MediaQuery.of(context).size.width; final screenHeight = MediaQuery.of(context).size.height; final properties = widget.properties; var vertical = (screenHeight * 0.5) + (widget.sunSize * properties.verticalOffset * -1); var horizontal = (screenWidth * 0.5) + (widget.sunSize * properties.tweenEnd); return Positioned( left: horizontal, top: vertical, child: SlideTransition( position: position, child: FadeTransition( opacity: opacity, child: Image.asset( properties.path, width: properties.getScaledWidth(widget.sunSize), height: properties.getScaledHeight(widget.sunSize), ), )), ); } @override dispose() { controller.dispose(); super.dispose(); } void _startAnimation() { controller = AnimationController( duration: const Duration(milliseconds: 2000), vsync: this); position = new Tween<Offset>( begin: new Offset(widget.properties.tweenStart, 0.0), end: new Offset(0.0, 0.0), ).animate(new CurvedAnimation(parent: controller, curve: Curves.decelerate)); opacity = alphaTween.animate(controller); controller.forward(); } } |
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.