HarmonyOS NEXT如何实现底部弹窗效果?

阅读 863
avatarAI BotBETA

在HarmonyOS NEXT中实现底部弹窗效果,可以通过以下步骤进行:

方法一:使用CustomDialog和自定义动画

  1. 定义CustomDialog

    • 创建一个CustomDialog组件,并设置其customStyle属性为true,以便自定义样式。
    • 在CustomDialog的build方法中定义弹窗的UI结构,如使用Column容器来构建底部弹出的视图。
  2. 设置动画效果

    • 利用ArkUI框架提供的动画功能,如TransitionEffect和animation方法,来自定义弹窗的显示和隐藏动画。
    • 可以使用translateY属性来实现从底部滑入和滑出的效果。
  3. 控制弹窗显示与隐藏

    • 通过控制CustomDialogController的open和close方法来控制弹窗的显示和隐藏。
    • 可以在UI组件(如Button)的onClick事件中调用这些方法。

方法二:使用半模态转场

  1. 定义半模态页面

    • 创建一个半模态页面,并设置其布局和样式。
    • 可以使用Stack布局来将半模态页面放置在屏幕底部。
  2. 绑定半模态页面

    • 通过bindSheet属性为某个组件(如Button)绑定半模态页面。
    • 当点击该组件时,将显示半模态页面。
  3. 自定义背景和内容

    • 可以设置半模态页面的背景为透明,以便只显示弹窗内容。
    • 在半模态页面中定义弹窗内容,如文本、按钮等元素的布局和样式。

示例代码

以下是一个使用CustomDialog实现底部弹窗效果的示例代码:

@CustomDialog
struct CustomDialogExample {
  controller?: CustomDialogController

  build() {
    Column() {
      Text('我是弹窗!')
        .width('100%')
        .height('35%')
        .backgroundColor(Color.White)
        .borderRadius(32)
    }
  }
}

@Entry
@Component
struct TextInputDemo {
  @State message: string = ''
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample(),
    autoCancel: true,
    alignment: DialogAlignment.Bottom,
    customStyle: true,
    isModal: true
  })

  build() {
    Column() {
      Button('显示弹窗')
        .margin(12)
        .onClick(() => {
          this.dialogController.open()
        })
    }
  }
}

在上述代码中,我们定义了一个CustomDialog组件CustomDialogExample,并在TextInputDemo组件中通过CustomDialogController来控制其显示和隐藏。通过设置alignment属性为DialogAlignment.Bottom,我们将弹窗的位置设置在屏幕底部。

注意事项

  • 确保CustomDialog的customStyle属性设置为true,以便自定义样式。
  • 在自定义弹窗的布局中,注意调整容器的大小和位置,以确保弹窗能够正确显示。
  • 如果需要更复杂的动画效果,可以结合ArkUI框架提供的其他动画功能进行实现。

通过以上方法,你可以在HarmonyOS NEXT中实现底部弹窗效果。

1 个回答

目前CustomDialog不支持修改左右和下方边距,可通过半模态转场来实现,请参考取下代码:

@Entry 
@Component 
struct BindSheetDemo { 
  // 半模态转场显示隐藏控制 
  @State isShowSheet: boolean = false; 
  private menuList: string[] = ['不要辣', '少放辣', '多放辣', '不要香菜', '不要香葱', '不要一次性餐具', '需要一次性餐具']; 
 
  // 通过@Builder构建半模态展示界面 
  @Builder 
  mySheet() { 
    Column() { 
      Flex({ direction: FlexDirection.Row, wrap: FlexWrap.Wrap }) { 
        ForEach(this.menuList, (item: string) => { 
          Text(item) 
            .fontSize(16) 
            .fontColor(0x333333) 
            .backgroundColor(0xf1f1f1) 
            .borderRadius(8) 
            .margin(10) 
            .padding(10) 
        }) 
      } 
      .padding({ top: 18 }) 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor(Color.White) 
  } 
 
  build() { 
    Column() { 
      Text('口味与餐具') 
        .fontSize(28) 
        .padding({ top: 30, bottom: 30 }) 
      Column() { 
        Row() { 
          Row() 
            .width(10) 
            .height(10) 
            .backgroundColor('#a8a8a8') 
            .margin({ right: 12 }) 
            .borderRadius(20) 
 
          Column() { 
            Text('选择点餐口味和餐具') 
              .fontSize(16) 
              .fontWeight(FontWeight.Medium) 
          } 
          .alignItems(HorizontalAlign.Start) 
 
          Blank() 
 
          Row() 
            .width(12) 
            .height(12) 
            .margin({ right: 15 }) 
            .border({ 
              width: { top: 2, right: 2 }, 
              color: 0xcccccc 
            }) 
            .rotate({ angle: 45 }) 
        } 
        .borderRadius(15) 
        .shadow({ radius: 100, color: '#ededed' }) 
        .width('90%') 
        .alignItems(VerticalAlign.Center) 
        .padding({ left: 15, top: 15, bottom: 15 }) 
        .backgroundColor(Color.White) 
 
        .bindSheet(this.isShowSheet, this.mySheet(), { 
          height: 300, 
          dragBar: false, 
          onDisappear: () => { 
            this.isShowSheet = !this.isShowSheet; 
          } 
        }) 
        .onClick(() => { 
          this.isShowSheet = !this.isShowSheet; 
        }) 
      } 
      .width('100%') 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor(0xf1f1f1) 
  } 
}
bindSheet可以有多个,同时bindSheet目前没有提供修改圆角的属性
多个bindSheet,请参考以下代码:
@Entry 
@Component 
struct BindSheetDemo { 
  // 半模态转场显示隐藏控制 
  @State isShowSheet: boolean = false; 
  @State isShowDetailSheet: boolean = false; 
  private menuList: string[] =  ['不要辣', '少放辣', '多放辣', '不要香菜', '不要香葱', '不要一次性餐具', '需要一次性餐具']; 
  private detailList: string[] = ['直播间111111', '相关内容22222222', '更多334455665']; 
  // 通过@Builder构建半模态展示界面 
  @Builder 
  mySheet() { 
    Column() { 
      Flex({ direction: FlexDirection.Row, wrap: FlexWrap.Wrap }) { 
        ForEach(this.menuList, (item: string) => { 
          Text(item) 
            .fontSize(16) 
            .fontColor(0x333333) 
            .backgroundColor(0xf1f1f1) 
            .borderRadius(8) 
            .margin(10) 
            .padding(10) 
            .onClick(() => { 
              this.isShowDetailSheet = !this.isShowDetailSheet; 
 
            }) 
        }) 
      } 
      .bindSheet(this.isShowDetailSheet, this.myDetailSheet(), { 
        // height: 550, 
        dragBar: true, 
        onDisappear: () => { 
          this.isShowDetailSheet = !this.isShowDetailSheet; 
        } 
      }) 
      .padding({ top: 18 }) 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor(Color.White) 
 
  } 
  @Builder 
  myDetailSheet() { 
    Column() { 
      Flex({ direction: FlexDirection.Row, wrap: FlexWrap.Wrap }) { 
        ForEach(this.detailList, (item: string) => { 
          Text(item) 
            .fontSize(16) 
            .fontColor(0x333333) 
            .backgroundColor(0xf1f1f1) 
            .borderRadius(8) 
            .margin(10) 
            .padding(10) 
        }) 
      } 
      .padding({ top: 18 }) 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor(Color.White) 
  } 
  build() { 
    Column() { 
      Text('直播间') 
        .fontSize(28) 
        .padding({ top: 30, bottom: 30 }) 
      Column() { 
        Row() { 
          Row() 
            .width(10) 
            .height(10) 
            .backgroundColor('#a8a8a8') 
            .margin({ right: 12 }) 
            .borderRadius(20) 
          Column() { 
            Text('点击查看') 
              .fontSize(16) 
              .fontWeight(FontWeight.Medium) 
          } 
          .alignItems(HorizontalAlign.Start) 
 
          Blank() 
 
          Row() 
            .width(12) 
            .height(12) 
            .margin({ right: 15 }) 
            .border({ 
              width: { top: 2, right: 2 }, 
              color: 0xcccccc 
            }) 
            .rotate({ angle: 45 }) 
        } 
        .borderRadius(15) 
        .shadow({ radius: 100, color: '#ededed' }) 
        .width('90%') 
        .alignItems(VerticalAlign.Center) 
        .padding({ left: 15, top: 15, bottom: 15 }) 
        .backgroundColor(Color.White) 
 
        .bindSheet(this.isShowSheet, this.mySheet(), { 
          height: 150, 
          dragBar: false, 
          onDisappear: () => { 
            this.isShowSheet = !this.isShowSheet; 
          } 
        }) 
        .onClick(() => { 
          this.isShowSheet = !this.isShowSheet; 
        }) 
      } 
      .width('100%') 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor(0xf1f1f1) 
 
  } 
}
同一个组件只能绑定一个bindsheet
同一个页面下多个弹窗,可以把bindsheet绑定在触发bindsheet显隐的组件上,参考代码如下:
@Entry 
@Component 
struct TEST { 
 
  @State isShowSheet1 :boolean =false 
  @State isShowSheet2 :boolean =false 
  build() { 
    Column(){ 
      Text('按钮1 ') 
        .bindSheet(this.isShowSheet1,this.Sheet1()) 
        .onClick(()=>{ 
 
          this.isShowSheet1 = true 
        }) 
      Text('按钮2 ') 
        .onClick(()=>{ 
 
          this.isShowSheet2 = true 
        }) 
        .bindSheet(this.isShowSheet2,this.Sheet2()) 
        .margin({top:40}) 
    } 
 
  } 
 
  @Builder 
  Sheet1(){ 
    Column(){ 
      Text('1') 
    } 
  } 
 
  @Builder 
  Sheet2(){ 
    Column(){ 
      Text('2') 
    } 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进