从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);
  }
}
发布了53 篇原创文章 · 获赞 20 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_32648731/article/details/89207392
今日推荐