HarmonyOS promptAction.openCustomDialog相关问题咨询

在开发中使用promptAction.openCustomDialog实现全局底部弹窗工具类,遇到些问题想请教下:

1.弹窗的宽度是否可以修改为全屏,目前看文档描述是“弹窗宽度在设备竖屏时默认为 所在窗口宽度 - 左右margin(16vp,设备为2in1时为40vp),最大默认宽度为400vp。” 尝试修改弹窗内容margin为-16vp,但不生效,是否有方法可以处理成宽度占满全屏?

2.设置alignment: DialogAlignment.Bottom,发现底部不是贴底,有安全区域,想问下怎么取消安全区域,让弹窗贴底弹出。通过offset设置偏移吗?还有没有其他方案?

3.设置底部弹窗动效,发现遮罩也跟随底部弹出。有什么办法可以让遮罩不动,值触发弹窗内容区域有动效?以下是示例代码:

promptAction.openCustomDialog({
  builder: () => {
    this.customDialogBuilder()
  },
  width: '100%',
  alignment: DialogAlignment.Bottom,
  isModal:true,
  offset: { dx: 0, dy: 20 },
  transition: TransitionEffect.move(TransitionEdge.BOTTOM).animation({
    duration: 800
  })
})
@Builder
customDialogBuilder() {
  Column() {
    Text('Title').fontSize(22)
    Text('Custom dialog Message').fontSize(12).margin({ top: 20 })
    Row() {
      Button("确认")
      Blank().width(50)
      Button("取消")
    }.margin({ top: 20 })
  }.margin({ left: -16, right: -16 })
}
HarmonyOS
18h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考示例:

//index.ets
import { CustomDialogA, CustomDialogB,webDialog } from '../cases/dialog/CustomDialog'
import { CaseDialog } from '../cases/dialog/CaseDialog'
import { AppRouter } from '../router/AppRouter'
@Entry
@Component
struct Index {
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()
  aboutToAppear() {
    AppRouter.getInstance().register(this.pageInfos);
  }
  @Builder
  PageMap(name: string) {
    if (name === 'dialog') {
      CaseDialog()
    } else if (name === 'customDialog') {
      CustomDialogA()
    } else if (name === 'fullDialog') {
      CustomDialogB()
    } else if(name === 'webDialog'){
      webDialog()
    }
  }
  build() {
    Navigation(this.pageInfos) {
      Column() {
        Button('Dialog', { stateEffect: true, type: ButtonType.Capsule })
          .onClick(() => {
            this.pageInfos.pushPath({ name: 'dialog' })
          })
      }
    }
    .hideTitleBar(true)
    .navDestination(this.PageMap)
  }
}
//AppRouter 
import { HashMap } from '@kit.ArkTS';
export interface RouterParams {
  name?: string
}
export class AppRouter {
  private pageStack?: NavPathStack;
  private routeMap: HashMap<string, WrappedBuilder<[object]>> = new HashMap();
  public static getInstance(): AppRouter {
    return instance;
  }
  register(pageStack: NavPathStack): void {
    this.pageStack = pageStack;
  }
  public addRouteMap(name: string, builder:
    WrappedBuilder<[object]>): void {
    this.routeMap.set(name, builder)
  }
  private pushPath(name: string): void {
    this.pageStack!.pushPath({ name: name })
  }
  public static push(name: string): void {
    instance.pushPath(name);
  }
  public static openDialog(name: string, params?: RouterParams): void {
    instance.pageStack?.pushPath({ name: name, param: params });
  }
  public static replace(name: string, params?: RouterParams): void {
    instance.pageStack?.replacePath({ name: name, param: params });
  }
  public static getParams(name: string): string {
    return JSON.stringify(instance.pageStack?.getParamByName(name));
  }
  public static pop(): void {
    instance.pageStack?.pop();
  }
}
let instance: AppRouter = new AppRouter();
export interface AnimateCallback {
  finish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
  start: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
  onFinish: ((isPush: boolean, isExit: boolean) => void | undefined) | undefined;
  timeout: (number | undefined) | undefined;
}
export class CustomTransition {
  static delegate = new CustomTransition();
  customTransitionMap: Map<string, AnimateCallback> = new Map()
  static getAnimateParam(name: string): AnimateCallback {
    let result: AnimateCallback = {
      start: CustomTransition.delegate.customTransitionMap.get(name)?.start,
      finish: CustomTransition.delegate.customTransitionMap.get(name)?.finish,
      timeout: CustomTransition.delegate.customTransitionMap.get(name)?.timeout,
      onFinish: CustomTransition.delegate.customTransitionMap.get(name)?.onFinish
    };
    return result;
  }
  static registerNavParam(name: string, startCallback: (operation: boolean, isExit: boolean) => void,
    endCallback: (operation: boolean, isExit: boolean) => void,
    onFinish: (opeation: boolean, isExit: boolean) => void, timeout: number): void {
    if (CustomTransition.delegate.customTransitionMap.has(name)) {
      let param = CustomTransition.delegate.customTransitionMap.get(name);
      if (param != undefined) {
        param.start = startCallback;
        param.finish = endCallback;
        param.timeout = timeout;
        param.onFinish = onFinish;
        return;
      }
    }
    let params: AnimateCallback = { timeout: timeout, start: startCallback, finish: endCallback, onFinish: onFinish };
    CustomTransition.delegate.customTransitionMap.set(name, params);
  }
}
//CustomDialog
import { AppRouter, RouterParams } from '../../router/AppRouter';
import web_webview from '@ohos.web.webview';
@Component
export struct CustomDialogA {
  @State heightSize: string = "50%";
  @State params: string = "";
  aboutToAppear() {
    this.params = AppRouter.getParams("customDialog");
  }
  build() {
    NavDestination() {
      Stack({ alignContent: Alignment.Bottom }) {
        Column() {
        }
        .width("100%")
        .height("100%")
        .backgroundColor('rgba(0,0,0,0.5)')
        .transition(
          TransitionEffect.OPACITY.animation({
            duration: 300,
            curve: Curve.Friction
          })
        )
        .onClick(() => {
          AppRouter.pop();
        })
        Column() {
          Text("弹窗")
            .fontColor(Color.White)
          Text("参数:" + this.params)
            .fontSize(18)
            .fontColor(Color.White)
          Button("新弹窗", { stateEffect: true, type: ButtonType.Capsule })
            .onClick(() => {
              AppRouter.openDialog("fullDialog", { name: "new Open" } as RouterParams)
            })
        }
        .width("100%")
        .backgroundColor(Color.Blue)
        .height(this.heightSize)
         .transition(
          TransitionEffect.move(TransitionEdge.BOTTOM)
            .animation({
              duration: 300,
              curve: Curve.Friction
            })
        )
      }
      .width("100%")
      .height("100%")
    }
    .mode(NavDestinationMode.DIALOG)
    .hideTitleBar(true)
  }
}
@Component
export struct CustomDialogB {
  @State params: string = "";
  aboutToAppear() {
    this.params = AppRouter.getParams("fullDialog");
  }
  build() {
    NavDestination() {
      Column() {
        Text("全屏弹窗")
        Text("参数:" + this.params)
          .fontSize(18)
        Button("返回", { stateEffect: true, type: ButtonType.Capsule })
          .onClick(() => {
            AppRouter.pop()
          })
      }
      .backgroundColor(Color.White)
      .width("100%")
      .height("100%")
      .transition(
        TransitionEffect.move(TransitionEdge.BOTTOM)
          .animation({
            duration: 300,
            curve: Curve.Friction
          })
      )
    }
    .mode(NavDestinationMode.DIALOG)
    .hideTitleBar(true)
  }
}
@Component
export struct webDialog {
  @State params: string = "";
  webviewController:web_webview.WebviewController = new web_webview.WebviewController();
  aboutToAppear() {
    this.params = AppRouter.getParams("webDialog");
  }
  build() {
    NavDestination() {
      Column() {
        Web({ src: 'www.huawei.com', controller: this.webviewController })
      }
    }
  }
}
//CaseDialog
import { AppRouter } from '../../router/AppRouter';
@Entry
@Component
export struct CaseDialog {
  @State message: string = "弹窗背景";
  build() {
    NavDestination() {
      Column() {
        Text(this.message)
        Row()
          .height("90%")
          .width("100%")
          .backgroundColor(Color.Brown)
        Button('打弹窗', { stateEffect: true, type: ButtonType.Capsule })
          .onClick(() => {
            // 打开组件弹窗
            AppRouter.openDialog("customDialog");
          })
      }
    }
  }
}
分享
微博
QQ
微信
回复
16h前
相关问题
promptAction.openCustomDialog 全局弹窗
501浏览 • 1回复 待解决
promptAction.openCustomDialog 自定义弹窗
362浏览 • 1回复 待解决