Flutter开屏广告

2020-09-24  本文已影响0人  demoly

Flutter广告页

项目需求

启动页显示完后,需要展示3秒广告页面

注意点

不要忘记配置路由

return MaterialApp(
      debugShowCheckedModeBanner: true,
      title: '名字',
      theme: LYAppTheme.norTheme,
      initialRoute: LYAd.routeName,
      routes: LYRoute.routes,
      onUnknownRoute: LYRoute.unknownRoute,
    );

直接上代码

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

class LYAd extends StatefulWidget {
  static const String routeName = '/ad';
  @override
  _LYAdState createState() => _LYAdState();
}

class _LYAdState extends State<LYAd> {

  Timer _timer;
  int count = 3;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        alignment: Alignment(1.0, -1.0), // 右上角对齐
        children: [
          ConstrainedBox(
            constraints: BoxConstraints.expand(),
            child: Image.asset('广告图片',//广告图
              fit: BoxFit.fill,
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(0.0, 30.0, 10.0, 0.0),
            child: FlatButton(
              color: Color.fromRGBO(0, 0, 0, 0.3),
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
              child: Text(
                "$count 跳过",
                style: TextStyle(color: Colors.white, fontSize: 15.0),
              ),
              onPressed: () {
                navigationPage();
              },
            ),
          )
        ],
      ),
    );
  }

  void startTime() async {
    //设置启动图生效时间
    var _duration = new Duration(seconds: 1);
    Timer(_duration, () {
      // 空等1秒之后再计时
      _timer = Timer.periodic(const Duration(milliseconds: 1000), (v) {
        count--;
        if (count == 0) {
          navigationPage();
        } else {
          setState(() {});
        }
      });
      return _timer;
    });
  }

  void navigationPage() {
    _timer.cancel();
    Navigator.of(context).pushReplacement(PageRouteBuilder(
        pageBuilder: (cxt,ani1,ani2){
          return FadeTransition(child: LYMain(),opacity: ani1);
        }
    ));
  }


}

上一篇下一篇

猜你喜欢

热点阅读