Flutter splash screen animation case AnimationController

Open an APP, often see beautiful start page, the start page is also known as the splash screen animation. It is animated from scratch and have a gradient of transparency. Later images show finished, it jumps to the user operable page.

AnimationController

AnimationControllerIs Animationa sub-class, it can be controlled Animation, which means that it is to control the animation, for example, control the execution time of the animation.

Here we have two parameters:

  • vsync:this : Vertical sync settings, this will be able to use.
  • duration : Duration of the animation, you can use this secondssecond, you can also use the millisecondsmillisecond, the work is often used in milliseconds, seconds, or because too rough.

Look at a section of code, the code is to control the animation of a typical application.

_controller = AnimationController(vsync: this,duration: Duration(milliseconds: 3000)); //3000毫秒
_animation = Tween(begin: 0.0, end: 1.0).animate(_controller);

This code means that the controller is provided an animation, this animation controller controls the animation execution time is 3000 milliseconds. Then we set up an animation, using animation conventions animation controller.

animation.addStatusListener

animation.addStatusListenerAnimation event listener, it can monitor the execution state of the animation, we only monitor the animation is completed, if the end of the page jump action is performed.

_animation.addStatusListener ((Status) {
       IF (Status == AnimationStatus.completed) { 
        Navigator.of (context) .pushAndRemoveUntil ( // jump to the next page, and the animation is finished off 
          MaterialPageRoute (Builder: (context) => FirstPage ()), 
          (route) => route == null 
        ); 
      } 
});
  • AnimationStatus.completed: That animation has finished.
  • pushAndRemoveUntil: Jump page, and the destruction of the current control.

The complete code is as follows:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch:Colors.blue,
      ),
      home:SplashScreen()
    );
  }
}

class SplashScreen extends StatefulWidget {
  _SplashScreenState createState() => _SplashScreenState();
}

classThe extends State _SplashScreenState <the SplashScreen> with SingleTickerProviderStateMixin { 
  AnimationController _controller; // controller, and generally control the animation time 
  Animation _animation; // animation
   // initialization state 
  @override
   void InitState () { 
    super.initState (); 
    _controller = AnimationController (VSYNC : the this , DURATION: the Duration (milliseconds: 3000 )); // 3000 ms 
    _animation = Tween (the begin: 0.0 , End: 1.0 ) .animate (_controller);
     // listening to events 
    _animation.addStatusListener ((status) {
      IF (Status == AnimationStatus.completed) { // determines that the motion has finished 
        Navigator.of (context) .pushAndRemoveUntil ( // jump to the next page, and the animation is finished off 
          MaterialPageRoute (Builder: (context) => FirstPage ( )), 
          (route) => route == null 
        ); 
      } 
    }); 
     _controller.forward (); // play the animation 
  }
   // controller destruction 
  @override
   void Dispose () { 
    _controller.dispose (); 
    super.dispose (); 
  } 
 
  @override 
  the Widget Build (BuildContext context) { 
    return FadeTransition (// animation transparent over 
      Opacity: _animation, 
      Child: Image.asset ( 
        ' Images / lake.jpg ' , 
        Scale: 2.0 , // image scaling 
        Fit: BoxFit.cover // filled full screen 
      ),     
    ); 
  } 
}

FirstPage.dart file

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
       appBar: AppBar(title: Text('闪屏动画'),),
       body: Center(
         child: Text('首页'),
       ),
    );
  }
}

 

Guess you like

Origin www.cnblogs.com/joe235/p/11236159.html