本文的目的为使用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")