入口文件
import 'package:flutter/material.dart';
import 'splash_screen.dart';
main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue
),
home: SplashScreen(),
);
}
}
方法文件
import 'package:flutter/material.dart';
import 'my_home_page.dart';
class SplashScreen extends StatefulWidget {
SplashScreen({Key key}) : super(key: key);
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin{
AnimationController _animationController;
Animation _animation;
// 初始化狀態(tài)
@override
void initState() {
super.initState();
// 兩個(gè)參數(shù) 垂直動(dòng)態(tài)演示 和顯示的秒數(shù)
_animationController = AnimationController(vsync: this,duration: Duration(milliseconds: 8000));
_animation = Tween(begin: 0.0,end: 1.0).animate(_animationController);
/*動(dòng)畫(huà)事件監(jiān)聽(tīng)器渣锦,
它可以監(jiān)聽(tīng)到動(dòng)畫(huà)的執(zhí)行狀態(tài)女揭,
這里只監(jiān)聽(tīng)動(dòng)畫(huà)是否結(jié)束空厌,
如果結(jié)束則執(zhí)行頁(yè)面跳轉(zhuǎn)動(dòng)作睡互。 */
_animation.addStatusListener((status){
// 判斷動(dòng)畫(huà)是否結(jié)束
if(status == AnimationStatus.completed){
Navigator.of(context).pushAndRemoveUntil(
// 動(dòng)畫(huà)結(jié)束跳轉(zhuǎn)到首頁(yè)
MaterialPageRoute(builder: (context)=> MyHomePage()),
(route) => route == null);
}
});
// 進(jìn)入頁(yè)面播放動(dòng)畫(huà)
_animationController.forward();
}
// 銷毀控制器
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// 動(dòng)畫(huà)透明的控件
return FadeTransition(
opacity: _animation,
// 隨便找的一張網(wǎng)絡(luò)圖片
child: Image.network('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1557146841827&di=59a69d473e5bb3633832f3b02073c29e&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20181203%2F32b39d8647304af6b3eb6d78c740ea4a.gif',
scale: 2.0,
fit: BoxFit.cover,),
);
}
}
首頁(yè)文件
import 'package:flutter/material.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('首頁(yè)'),),
body: Center(
child: Text('我是首頁(yè)'),
),
);
}
}