Flutter achieves animation effects through Animation components
The content in the main.dart file is the same as the code shown in the previous article.
This blog focuses on the animation effect of zooming in and out.
1. WelcomeScreen widget inherits the StatefulWidget component.
Setting animation effect in initState method
2. The animation effect mainly passes through
Implementation of Animation Component
Animation Controller sets the duration of animation execution
controller = AnimationController( duration: Duration(seconds: 1), vsync: this); //AnimationController
Curved Animation Sets the Effect of Animation
animation = CurvedAnimation(parent: controller, curve: Curves.decelerate);
controller.forward() sets the execution of the animation and adds listeners
The state after controller.forward() is complete.
AnimationStatus.completed
controller.reverse(from: 1.0);
The subsequent state is lost, i.e.
AnimationStatus.dismissed
controller.forward(); animation.addStatusListener((status){ if (status == AnimationStatus.completed){ controller.reverse(from: 1.0); }else if (status == AnimationStatus.dismissed){ controller.forward(); } }); controller.addListener(() { setState(() {}); print(animation.value); });
The complete code is as follows:
import 'package:flutter/material.dart'; import 'login_screen.dart'; import 'registration_screen.dart'; class WelcomeScreen extends StatefulWidget { static String id = 'welcome_screen'; @override _WelcomeScreenState createState() => _WelcomeScreenState(); } class _WelcomeScreenState extends State<WelcomeScreen> with SingleTickerProviderStateMixin { AnimationController controller; Animation animation; @override void initState() { super.initState(); controller = AnimationController( duration: Duration(seconds: 1), vsync: this); //AnimationController // animation = ColorTween(begin: Colors.blueGrey, end: Colors.white) // .animate(controller); animation = CurvedAnimation(parent: controller, curve: Curves.decelerate); controller.forward(); animation.addStatusListener((status){ if (status == AnimationStatus.completed){ controller.reverse(from: 1.0); }else if (status == AnimationStatus.dismissed){ controller.forward(); } }); controller.addListener(() { setState(() {}); print(animation.value); }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: Padding( padding: EdgeInsets.symmetric(horizontal: 24.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[ Row( children: <Widget>[ Hero( tag: 'logo', child: Container( child: Image.asset('images/logo.png'), height: animation.value * 100, ), ), Text( 'Falsh Chat', style: TextStyle( fontSize: 45.0, fontWeight: FontWeight.w900, ), ), ], ), SizedBox( height: 48.0, ), Padding( padding: EdgeInsets.symmetric(vertical: 16.0), child: Material( elevation: 5.0, color: Colors.lightBlueAccent, borderRadius: BorderRadius.circular(30.0), child: MaterialButton( onPressed: () { Navigator.pushNamed(context, LoginScreen.id); }, minWidth: 200.0, height: 42.0, child: Text( 'Log In', ), ), ), ), Padding( padding: EdgeInsets.symmetric(vertical: 16.0), child: Material( color: Colors.blueAccent, borderRadius: BorderRadius.circular(30.0), elevation: 5.0, child: MaterialButton( onPressed: () { Navigator.pushNamed(context, RegistrationScreen.id); }, minWidth: 200.0, height: 42.0, child: Text( 'Register', ), ), ), ), ], ), ), ); } }
The whole effect is shown in the following figure: