Import images

Create a new images directory in the lib sibling directory and place the splash screen image.

Updating the Configuration File

Open the pubspec.yaml file and add the following ASSTes:

assets:
-images/splash_girl.png
Copy the code

Create two pages

Create a HomePage and SplashPage in the lib file as the HomePage and SplashPage, respectively.

Write the mian. Dart

Dart does the logic to launch the splash screen directly and provides a route to the home page.

import 'package:flutter/material.dart';
import 'SplashPage.dart';
import 'HomePage.dart';

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

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Todo', theme: new ThemeData(primarySwatch: color.blue,), home: new SplashPage(), // routes: <String, WidgetBuilder>{// route'/HomePage': (BuildContext context) => new HomePage() }, ); }}Copy the code

The MaterialApp control is used here, home is the default page, routes is the configured route.

Write SplashPage. Dart

Create a StatefulWidget that starts counting down after initState and enters a HomePage. The asynchronous countdown is handled using Dart’s Future.

import 'dart:async';
import 'package:flutter/widgets.dart';

class SplashPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _SplashPageState();
}
}

class _SplashPageState extends State<SplashPage> {
@override
Widget build(BuildContext context) {
return new Image.asset("images/splash_girl.png");
}

@override
void initState() { super.initState(); countDown(); } // countdown voidcountDown() {
var _duration = new Duration(seconds: 3);
new Future.delayed(_duration, go2HomePage);
}

void go2HomePage() {
Navigator.of(context).pushReplacementNamed('/HomePage'); }}Copy the code

Write a homepage.dart

The text “Home Page” is displayed in the middle of the Home Page.

import 'package:flutter/widgets.dart';

class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new _HomePage();
}
}

class _HomePage extends State<HomePage> {
@override
Widget build(BuildContext context) {
return new Center(
child: new Text("Home Page")); }}Copy the code

Reference:

Flutter control MaterialApp:blog.chengyunfeng.com/?p=1041

The Dart asynchronous processing: segmentfault.com/a/119000000…