Flutter开发-启动页
https://flutter.cn/docs/development/ui/advanced/splash-screen
安卓启动页
在android\app\src\main\res\drawable-v21\launch_background.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0″ encoding=" utf-8″?> <!– Modify this file to customize your launch splash screen –> <layer-list xmlns:android ="http://schemas.android.com/apk/res/android" > <item android:drawable ="@android:color/white" /> <!– You can insert your own image assets here –> //这样写可以通过图片铺满整个屏幕,不同屏幕可能会有拉伸 <item android:drawable ="@mipmap/splash_bg" > </layer-list >
将- 里面的内容反注释,图片分辨率1080 x 1920放入android\app\src\main\res\mipmap-xxhdpi。
背景颜色,因为Android自带的颜色只有white和black。
想自定义颜色,需要把原来的 @android:color/white”中的android:去掉即可。
去掉以后为
1 <item android:drawable="@color/other" />
其中other是自定义的颜色。
首先需要在android\app\src\main\res\values下新建一个colors.xml文件,
内容为
1 2 3 4 5 <?xml version="1.0″ encoding=" utf-8″?> <resources > <color name ="other" > #0397ff</color > <color name ="transparent" > #00000000</color > </resources >
IOS启动页
Flutter启动页 配置图片视频资源
1 2 3 4 5 6 7 8 9 10 flutter: uses-material-design: true assets: - assets/videos/aeologic_logo.mp4 - assets/images/aeologic_logo.png - assets/images/animation.png - assets/images/image.png - assets/images/video.png - assets/images/logo.png - assets/images/powered_by.png
静态 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 import 'package:flutter/material.dart' ;import 'package:test2/splash/ImageSplashScreen.dart' ;void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Splash Demo' , theme: ThemeData( primarySwatch: Colors.blue, ), home: ImageSplashScreen(), routes: <String , WidgetBuilder>{ "/home" : (BuildContext context) => MyHomePage(title: 'Flutter Demo Home Page' ), }, ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required this .title}) : super (key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State <MyHomePage > { int _counter = 0 ; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'You have pushed the button this many times:' , ), Text( '$_counter ' , style: Theme.of(context).textTheme.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment' , child: Icon(Icons.add), ), ); } }
ImageSplashScreen
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 import 'dart:async' ;import 'package:flutter/cupertino.dart' ;import 'package:flutter/material.dart' ;class ImageSplashScreen extends StatefulWidget { @override SplashScreenState createState() => SplashScreenState(); } class SplashScreenState extends State <ImageSplashScreen > { startTime() async { var _duration = Duration (seconds: 2 ); return Timer(_duration, navigationPage); } void navigationPage() { Navigator.of(context).pushReplacementNamed("/home" ); } @override void initState() { super .initState(); startTime(); } @override Widget build(BuildContext context) { return Scaffold( body: Stack( fit: StackFit.expand, children: <Widget>[ Image.asset( 'assets/images/aeologic_logo.png' , fit: BoxFit.fill, width: double .infinity, height: double .infinity, ) ], ), ); } }
动态 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 import 'package:flutter/material.dart' ;import 'package:test2/splash/AnimatedSplashScreen.dart' ;void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Splash Demo' , theme: ThemeData( primarySwatch: Colors.blue, ), home: AnimatedSplashScreen(), routes: <String , WidgetBuilder>{ "/home" : (BuildContext context) => MyHomePage(title: 'Flutter Demo Home Page' ), }, ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required this .title}) : super (key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State <MyHomePage > { int _counter = 0 ; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'You have pushed the button this many times:' , ), Text( '$_counter ' , style: Theme.of(context).textTheme.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment' , child: Icon(Icons.add), ), ); } }
AnimatedSplashScreen
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 import 'dart:async' ;import 'package:flutter/cupertino.dart' ;import 'package:flutter/material.dart' ;class AnimatedSplashScreen extends StatefulWidget { @override SplashScreenState createState() => SplashScreenState(); } class SplashScreenState extends State <AnimatedSplashScreen > with SingleTickerProviderStateMixin { var _visible = true ; late AnimationController animationController; late Animation<double > animation; startTime() async { var _duration = Duration (seconds: 3 ); return Timer(_duration, navigationPage); } void navigationPage() { Navigator.of(context).pushReplacementNamed("/home" ); } @override void initState() { super .initState(); animationController = AnimationController( vsync: this , duration: Duration (seconds: 2 )); animation = CurvedAnimation(parent: animationController, curve: Curves.easeOut); animation.addListener(() => this .setState(() {})); animationController.forward(); setState(() { _visible = !_visible; }); startTime(); } @override Widget build(BuildContext context) { return Scaffold( body: Stack( fit: StackFit.expand, children: <Widget>[ Column( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: <Widget>[ Padding( padding: EdgeInsets.only(bottom: 50.0 ), child: Image.asset( 'assets/images/powered_by.png' , height: 30.0 , fit: BoxFit.scaleDown, )) ], ), Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Image.asset( 'assets/images/logo.png' , width: animation.value * 250 , height: animation.value * 250 , ), ], ), ], ), ); } }
视频 使用插件
1 2 dependencies: video_player: ^2.1.15
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 import 'package:flutter/material.dart' ;import 'package:test2/splash/VideoSplashScreen.dart' ;void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Splash Demo' , theme: ThemeData( primarySwatch: Colors.blue, ), home: VideoSplashScreen(), routes: <String , WidgetBuilder>{ "/home" : (BuildContext context) => MyHomePage(title: 'Flutter Demo Home Page' ), }, ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required this .title}) : super (key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State <MyHomePage > { int _counter = 0 ; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'You have pushed the button this many times:' , ), Text( '$_counter ' , style: Theme.of(context).textTheme.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment' , child: Icon(Icons.add), ), ); } }
VideoSplashScreen
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 import 'dart:async' ;import 'package:flutter/cupertino.dart' ;import 'package:flutter/material.dart' ;import 'package:video_player/video_player.dart' ;class VideoSplashScreen extends StatefulWidget { @override VideoState createState() => VideoState(); } class VideoState extends State <VideoSplashScreen > { late VideoPlayerController playerController; late VoidCallback listener; @override void initState() { super .initState(); listener = () { setState(() {}); }; initializeVideo(); playerController.play(); startTime(); } startTime() async { var _duration = Duration (seconds: 5 ); return Timer(_duration, navigationPage); } void navigationPage() { playerController.setVolume(0.0 ); playerController.removeListener(listener); Navigator.of(context).pushReplacementNamed("/home" ); } void initializeVideo() { playerController = VideoPlayerController.asset('assets/videos/aeologic_logo.mp4' ) ..addListener(listener) ..setVolume(1.0 ) ..initialize() ..play(); } @override void deactivate() { if (playerController != null ) { playerController.setVolume(0.0 ); playerController.removeListener(listener); } super .deactivate(); } @override void dispose() { if (playerController != null ) playerController.dispose(); super .dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: Stack(fit: StackFit.expand, children: <Widget>[ AspectRatio( aspectRatio: 9 / 16 , child: Container( child: (playerController != null ? VideoPlayer( playerController, ) : Container()), )), ])); } }
Flutter引导页 Flutter广告页