Flutter Dialog 在小米,OPPO,某些机型上意外遮罩阴影

在使用Flutter为contentView的Dialog中,在某些机型上,Dialog下层的阴影遮罩遮住了Dialog本身。具体原因不详。这里提供一种解决方式:

  • 1.为Dialog去掉原来的阴影效果
     private void showInBottom() {
       //未解决flutter dialog在某些机型下阴影遮罩挡住了对话框        
       activity.addShadow();//手动展示阴影
       
       Window window = getWindow();
       WindowManager.LayoutParams layoutParams = null;
       if (window != null) {
           layoutParams = window.getAttributes();
           layoutParams.width = ToolBox.getDisplayWith();
           layoutParams.height = (int) getContext().getResources().getDimension(R.dimen.x434);
           window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
           window.setGravity(Gravity.BOTTOM);
       }

   }
     @Override
     public void show() {
           showInBottom();
           super.show();

      }

     @Override
     public void dismiss() {
           activity.removeShadow();
           super.dismiss();
      }
  • 2.在activity中手动添加阴影
//解决flutter dialog在某些机型下阴影遮罩挡住了对话框而手动添加阴影
   public void addShadow() {
       Window window = getWindow();
       if (window != null) {
           ViewGroup contentView = window.findViewById(Window.ID_ANDROID_CONTENT);
           if (mShadowView == null) {
               mShadowView = new View(this);
           }
           mShadowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
           mShadowView.setBackgroundColor(ToolBox.getResources().getColor(R.color.c_66000000));
           try {
               contentView.addView(mShadowView);
           } catch (Exception e) {
               e.printStackTrace();
           }

       }
   }

   //解决flutter dialog在某些机型下阴影遮罩挡住了对话框
   public void removeShadow() {
       Window window = getWindow();
       if (window != null) {
           ViewGroup contentView = window.findViewById(Window.ID_ANDROID_CONTENT);
           if (mShadowView != null) {
               try {
                   contentView.removeView(mShadowView);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       }
   }

你可能感兴趣的:(Flutter Dialog 在小米,OPPO,某些机型上意外遮罩阴影)