flutter 廣告頁-3秒讀數后或者點擊跳過廣告按鈕后跳轉
/** * 廣告頁,3秒自動跳轉到首頁 */ import 'dart:async'; import 'package:flutter/material.dart'; 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), ), ), ) ], ); } }