从Android 开发到Flutter 之 开屏页和广告页《二》

在APP开发的过程中,我们一般需要一个开屏页和一个广告页

开屏页

开发Android 的都知道,Android启动的时候会有一个小的空白,我们的解决方法是给开屏页设置style ,让其在加载声明周期方法之前先设置一个填位图,消除空白


  • Flutter 项目在IOS上是秒起的,不会出现这个问题,在Android上还是会出现的,但是Flutter 项目已经将这个操作在创建项目的时候就实现了

  • 打开android moudle 找到res->style 看一下启动页的主题 drawable 所指向的文件 launch_background 文件,就是更具Android中常用的解决方法处理的,

<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <!-- Show a splash screen on the activity. Automatically removed when
             Flutter draws its first frame -->
        <!--<item name="android:windowIsTranslucent">true</item>-->
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
  • 找到launch_background
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/splash" /> <--此处为更换的图片-->

    <!-- You can insert your own image assets here -->
   <!--<item >-->
        <!--<bitmap-->
            <!--android:gravity="center"-->
            <!--android:src="@mipmap/splash"-->
            <!--android:tileMode="disabled"-->
            <!--android:autoMirrored="true"-->
            <!--/>-->
    <!--</item>-->
</layer-list>

这就解决了开屏的问题,但是一般会在开屏的时候初始化一些东西 这样的话我们怎么处理,此时我们可以使用开屏之后开启广告页 既展示广告又能初始化一些东西

需求:APP启动的时候需要判断是否已经登录,跳转相对应的page

在设置完成开屏之后会发现启动直接就到main.dart了,没办法做这个处理,并且Dart 是单线程的语言,关于数据的存储都需要异步来实现,

解决办法

添加广告页,既作为展示广告页初始化一些操作

class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  String route;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getCredential();
  }

  getCredential() {
    SPUtil.init().get(TMCommonConstant.AUTH_TOKEN).then((result) {
      route = result;
      //异步获取到数据之后判断token 是否为空
      if (route == null) {
        route = "/";
      } else {
        route = "navigation";
      }
      countDown();
    });
  }

  @override
  Widget build(BuildContext context) {
  	//展示广告
    return new Image.asset(
      Images.splash,
      fit: BoxFit.fill,
    );
  }

  void countDown() {
  //倒计时3s
    var _duration = new Duration(seconds: 3);
    Future.delayed(_duration, goHome);
  }

  void goHome() {
  //根据判断的路由跳转到相应的页面
    Navigator.pushReplacementNamed(context, route);
  }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值