flutter 启动页面之后的广告页

3秒之后自动跳转到主页面:

/**
 * 广告页,3秒自动跳转到首页
 */

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:zybdapp/youshang/main.dart';

void main() {
  runApp(new MaterialApp(
    title: 'ad',
    home: new SplashScreen(),
    routes: {
      '/main': (BuildContext context) => new MainScreen() // 首页widget类,省略了
    },
  ));
}

//class MyApp extends StatelessWidget {
//  @override
//  Widget build(BuildContext context) {
//    return new Stack(
//      alignment: const Alignment(1.0, -1.0), // 右上角对齐
//      children: [
//        new ConstrainedBox(
//          constraints: BoxConstraints.expand(),
//          child: new Image.asset(
//            "assets/images/ad.png",
//            fit: BoxFit.fill,
//          ),
//        ),
//      ],
//    );
//  }
//}

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

class _SplashScreenState extends State {
  Timer _timer;

  startTime() async {
    //设置启动图生效时间
    var _duration = new Duration(seconds: 3);
    _timer = new Timer(_duration, navigationPage);
    return _timer;
  }

  void navigationPage() {
    _timer.cancel();
    Navigator.of(context).pushReplacementNamed('/main');
  }

  @override
  void initState() {
    super.initState();
    startTime();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ConstrainedBox(
        constraints: BoxConstraints.expand(),
        child: new Image.asset(
          "assets/images/ad.png",
          fit: BoxFit.fill,
        ),
      ),
    );
  }
}

你可能感兴趣的:(flutter 启动页面之后的广告页)