Flutter启动画面插件splashscreen的使用
Flutter启动画面插件splashscreen的使用
Splash Screen
一个用于任何Flutter应用的启动画面插件,可以轻松地自定义。
截图
使用
如何添加依赖
在pubspec.yaml
文件中添加splashscreen依赖:
dependencies:
flutter:
sdk: flutter
splashscreen:
如何使用
new SplashScreen(
seconds: 14,
navigateAfterSeconds: new AfterSplash(),
title: new Text('欢迎进入SplashScreen'),
image: new Image.asset('screenshot.png'),
backgroundColor: Colors.white,
styleTextUnderTheLoader: new TextStyle(),
photoSize: 100.0,
loaderColor: Colors.red
);
示例代码
以下是一个完整的示例代码,展示了如何使用splashscreen插件。
import 'package:flutter/material.dart';
import 'package:splashscreen/splashscreen.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
[@override](/user/override)
Widget build(BuildContext context) {
return new SplashScreen(
seconds: 14,
navigateAfterSeconds: new AfterSplash(),
title: new Text(
'欢迎进入SplashScreen',
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
),
image: new Image.network(
'https://flutter.io/images/catalog-widget-placeholder.png'),
backgroundColor: Colors.white,
loaderColor: Colors.red,
);
}
}
class AfterSplash extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("欢迎进入SplashScreen插件"),
automaticallyImplyLeading: false,
),
body: new Center(
child: new Text(
"成功!",
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0),
),
),
);
}
}
示例
基于时间的示例
import 'package:flutter/material.dart';
import 'package:splashscreen/splashscreen.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
[@override](/user/override)
Widget build(BuildContext context) {
return new SplashScreen(
seconds: 14,
navigateAfterSeconds: new AfterSplash(),
title: new Text(
'欢迎进入SplashScreen',
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
),
image: new Image.network(
'https://flutter.io/images/catalog-widget-placeholder.png'),
backgroundColor: Colors.white,
loaderColor: Colors.red,
);
}
}
class AfterSplash extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("欢迎进入SplashScreen插件"),
automaticallyImplyLeading: false,
),
body: new Center(
child: new Text(
"成功!",
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0),
),
),
);
}
}
基于未来的示例
import 'package:flutter/material.dart';
import 'package:splashscreen/splashscreen.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
Future<Widget> loadFromFuture() async {
// 模拟从服务器获取数据
await Future.delayed(Duration(seconds: 5));
return Future.value(new AfterSplash());
}
[@override](/user/override)
Widget build(BuildContext context) {
return new SplashScreen(
navigateAfterFuture: loadFromFuture(),
title: new Text(
'欢迎进入SplashScreen',
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
),
image: new Image.network(
'https://flutter.io/images/catalog-widget-placeholder.png'),
backgroundColor: Colors.white,
loaderColor: Colors.red,
);
}
}
class AfterSplash extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("欢迎进入SplashScreen插件"),
automaticallyImplyLeading: false,
),
body: new Center(
child: new Text(
"成功!",
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0),
),
),
);
}
}
动画主图像
你可以通过给主图像添加splashscreenImage
标签来实现动画效果。例如,在目标页面中添加相同的标签,这样主图像就会从启动画面动画到目标页面。
添加自定义页面过渡
你可以使用pageRoute
来自定义页面过渡效果。例如:
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
[@override](/user/override)
Widget build(BuildContext context) {
return new SplashScreen(
seconds: 14,
title: new Text('欢迎进入SplashScreen'),
image: new Image.network(
'https://flutter.io/images/catalog-widget-placeholder.png'),
backgroundColor: Colors.white,
loaderColor: Colors.red,
pageRoute: _createRoute()
);
}
}
Route _createRoute() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => Page2(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
var begin = Offset(0.0, 1.0);
var end = Offset.zero;
var curve = Curves.ease;
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
return SlideTransition(
position: animation.drive(tween),
child: child,
);
},
);
}
更多关于Flutter启动画面插件splashscreen的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter启动画面插件splashscreen的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,启动画面(Splash Screen)是应用程序启动时显示的初始屏幕。虽然Flutter本身没有内置的启动画面功能,但你可以使用第三方插件来实现这一功能。splashscreen
是一个常用的Flutter插件,用于创建启动画面。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 splashscreen
插件的依赖:
dependencies:
flutter:
sdk: flutter
splashscreen: ^1.3.5
然后运行 flutter pub get
来获取依赖。
2. 使用 SplashScreen
在你的 main.dart
文件中,你可以使用 SplashScreen
来创建一个启动画面。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:splashscreen/splashscreen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Splash Screen',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SplashScreen(
seconds: 3,
navigateAfterSeconds: HomeScreen(),
title: Text(
'Welcome to My App',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white,
),
),
image: Image.asset('assets/splash.png'),
backgroundColor: Colors.blue,
styleTextUnderTheLoader: TextStyle(),
photoSize: 100.0,
loaderColor: Colors.white,
),
);
}
}
class HomeScreen extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Screen'),
),
body: Center(
child: Text('Welcome to the Home Screen!'),
),
);
}
}