flutter 廣告頁-3秒讀數(shù)后或者點(diǎn)擊跳過廣告按鈕后跳轉(zhuǎn)

/**
 * 廣告頁,3秒自動(dòng)跳轉(zhuǎn)到首頁
 */

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 {
    //設(shè)置啟動(dòng)圖生效時(shí)間
    var _duration = new Duration(seconds: 1);
    new Timer(_duration, () {
      // 空等1秒之后再計(jì)時(shí)
      _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), // 右上角對(duì)齊
      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),
            ),
          ),
        )
      ],
    );
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容