美文网首页
Flutter 学习中遇到问题的一些分享

Flutter 学习中遇到问题的一些分享

作者: yivin | 来源:发表于2019-07-13 15:49 被阅读0次

分享一些我在学习过程中遇到的问题

1.Android启动白屏

一些配置比较低的手机上运行测试包时会有一个非常久的启动白屏,当然打成正式包后会极大的缩短这个时间,但还是很明显的。
在安卓原生开发中我们都需要对apk启动速度进行优化,这需要从很多方向去考虑,其中有一个就是闪屏图片。我们看到的启动白屏就是没有修改过的闪屏,在flutter工程的android包里,我们找到AndroidManif.xml文件,里面的安卓样式设置是:

        <activity
            ...
            android:theme="@style/LaunchTheme"
            ...
           >

这个样式是在res/values/styles.xml里

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <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:windowBackground">@drawable/launch_background</item>
    </style>
</resources>

可以看到这个设置就是在绘制出第一帧前显示的一个drawable,看看drawable里写了什么

<?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="@android:color/white" />

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

很简单的一个白色背景,结合官方下面给出的注释,可以自己添加一个图片进来。
所以想解决启动时那个难看的白屏,只需要在这里加上背景图作为启动闪屏背景就行了,如果想做一些动效和炫酷的闪屏过渡效果,大家可以参考一些原生的闪屏优化文章。

2.Flutter工程里看安卓代码各种飘红

有时候我们需要在安卓包里去修改一些代码,或者编写一些方法时往往打开后发现满屏的红色警告,而且还没有联想和提示,其实只要点击右上角的


或者在安卓文件上右击选择



就会生成一个新的工作空间,你在那边修改的代码会自动同步到flutter工程中

3.Text文字下划线

Text的decoration默认是TextDecoration.none
不知道什么原因,有时候定义的Text下面会出现两条黄色的下划线,明明没有设置decoration的样式,此时要去掉下面的下划线只需要在style下面的TextStyle增加属性decoration: TextDecoration.none

4.Flutter中的动画
..addStatusListener((state) {

        print("animationstate:${state}");

        if(state == AnimationStatus.completed){

          animationController.reverse();

        }else if(state == AnimationStatus.dismissed){

          animationController.forward();

        }

      });

animationController.forward();

forward对应completed

reverse对应dismissed

5.组件通信

flutter组件的通信方式有很多种,比如回调,eventbus,Notification等。
用Notification时要注意dispatch方法中传的context必须和他监听的context是同一个Widget的,否则是不会回调的,可以在监听的组件中赋值给GlobalKey,然后在发送的时候传入key.currentContext。
GlobalKey会分配一个唯一的key,并且可以通过key.currentContext获取到这个key的context

以上是学习过程中自己总结的,如有错误希望指出,在后面的学习中会持续更新

相关文章

网友评论

      本文标题:Flutter 学习中遇到问题的一些分享

      本文链接:https://www.haomeiwen.com/subject/slfmkctx.html