美文网首页
flutter 广告页-3秒读数后或者点击跳过广告按钮后跳转

flutter 广告页-3秒读数后或者点击跳过广告按钮后跳转

作者: 王保全_1098 | 来源:发表于2018-11-18 14:44 被阅读0次
    /**
     * 广告页,3秒自动跳转到首页
     */
    
    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:zybdapp/youshang/main_screen.dart';
    
    void main() {
      runApp(new MaterialApp(
        title: 'ad',
        home: new SplashScreen(),
        routes: <String, WidgetBuilder>{
          '/main': (BuildContext context) => new MainScreen()
        },
      ));
    }
    
    class SplashScreen extends StatefulWidget {
      @override
      _SplashScreenState createState() => new _SplashScreenState();
    }
    
    class _SplashScreenState extends State<SplashScreen> {
      Timer _timer;
      int count = 3;
    
      startTime() async {
        //设置启动图生效时间
        var _duration = new Duration(seconds: 1);
        new Timer(_duration, () {
          // 空等1秒之后再计时
          _timer = new Timer.periodic(const Duration(milliseconds: 1000), (v) {
            count--;
            if (count == 0) {
              navigationPage();
            } else {
              setState(() {});
            }
          });
          return _timer;
        });
      }
    
      void navigationPage() {
        _timer.cancel();
        Navigator.of(context).pushReplacementNamed('/main');
      }
    
      @override
      void initState() {
        super.initState();
        startTime();
      }
    
      @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.jpg",
                fit: BoxFit.fill,
              ),
            ),
            new Padding(
              padding: new EdgeInsets.fromLTRB(0.0, 30.0, 10.0, 0.0),
              child: new FlatButton(
                onPressed: () {
                  navigationPage();
                },
    //            padding: EdgeInsets.all(0.0),
                color: Colors.grey,
                child: new Text(
                  "$count 跳过广告",
                  style: new TextStyle(color: Colors.white, fontSize: 12.0),
                ),
              ),
            )
          ],
        );
      }
    }
    
    

    相关文章

      网友评论

          本文标题:flutter 广告页-3秒读数后或者点击跳过广告按钮后跳转

          本文链接:https://www.haomeiwen.com/subject/jslgfqtx.html