Flutter 使用SafeArea终结沉浸式状态栏、刘海屏与虚拟键之乱(竖屏篇)

本文的目的为使用Flutter技术时,实现如下效果:
1、安卓或苹果手机为刘海屏手机、虚拟键手机时,在竖屏状态下正常显示刘海屏和虚拟键,沉浸式状态栏
2、之外的手机全屏显示,没有状态栏,虚拟键正常显示

详细步骤:

1、安卓端设置
包括识别O版部分机型的刘海屏,P版的所有刘海屏并进行设置。状态栏背景颜色设置为透明


    private void useSafeArea(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            getHighNotchParams();
        } else {
            getLowNotchParams(context);
        }
    }

    public void getLowNotchParams(Context context) {
        if (hasNotchInHuawei(context)) {
            _topHeight = getNotchSize(context);
        } else if (hasNotchInOppo(context)) {
            _topHeight = getStatusBarHeight(context);
        } else if (hasNotchInVivo(context)) {
            _topHeight = getStatusBarHeight(context);
        } else if (getInt("ro.miui.notch", context) == 1) {
            int resourceId = context.getResources().getIdentifier("notch_height", "dimen", "android");
            if (resourceId > 0) {
                _topHeight = context.getResources().getDimensionPixelSize(resourceId);
            }
        }

        lastSet(_topHeight > 0);
    }

    // 是否是小米手机
    public static boolean isXiaomi() {
        return "Xiaomi".equals(Build.MANUFACTURER);
    }

    /**
     * 小米刘海屏判断.
     *
     * @return 0 if it is not notch ; return 1 means notch
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static int getInt(String key, Context context) {
        int result = 0;
        if (isXiaomi()) {
            try {
                ClassLoader classLoader = context.getClassLoader();
                @SuppressWarnings("rawtypes")
             
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值