HarmonyOS鸿蒙Next中底部弹出的弹窗用哪个组件好

HarmonyOS鸿蒙Next中底部弹出的弹窗用哪个组件好 底部弹出的弹窗用哪个组件好,最好有demo

4 回复

推荐使用半模态转场,参考文档,文档里有示例

文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-sheet-transition-V5

或者可以通过自定义弹框实现:

let anmDuration: number = 300;

// 弹窗交互

@CustomDialog
struct CustomDialogExample {
  controller: CustomDialogController = new CustomDialogController({

    builder: CustomDialogExample({}),
    autoCancel: false

  })
  @State showFlag: Visibility = Visibility.Visible;
  @State isAutoCancel: boolean = false;
  textController: TextAreaController = new TextAreaController()

  build() {

    Column() {

      Row() {

        Text("自定义动画的弹窗")

      }

      .borderRadius(20)

      .backgroundColor('#ff6288cb')

      .height(200)

      .width('100%')

    }
    .margin({ top: 10 })

    .justifyContent(FlexAlign.Center)

    .width('100%')

    .height("100%")

    .onClick(() => {

      console.log("dialogClick")

      if (this.isAutoCancel) {

        console.log("dialogClick2")

        this.cancel();

      }

    })

    .visibility(this.showFlag)

    // 定义进场出场转场动画效果

    .transition(TransitionEffect.OPACITY.animation({ duration: anmDuration })

      .combine(TransitionEffect.translate({ y: 500 })))

  }

  // 延迟关闭弹窗,让自定义的出场动画显示

  cancel() {

    this.showFlag = Visibility.Hidden

    console.log("closeDialog")

    setTimeout(() => {

      this.controller.close()

    }, anmDuration)

  }
}

@Entry
@Component
struct CustomDialogUser {
  @State isAutoCancel: boolean = true;
  dialogController: CustomDialogController = new CustomDialogController({

    builder: CustomDialogExample({ isAutoCancel: this.isAutoCancel }),

    autoCancel: this.isAutoCancel,

    customStyle: true,

  })

  build() {

    Column() {

      Button('click me')

        .onClick(() => {

          this.dialogController.open()

        })

    }.width('100%')

    .height('100%')

  }
}

更多关于HarmonyOS鸿蒙Next中底部弹出的弹窗用哪个组件好的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,底部弹出的弹窗可以使用BottomSheet组件。BottomSheet是鸿蒙系统中专门用于从屏幕底部向上滑出的弹窗组件,支持多种交互模式,如模态和非模态弹窗。它可以通过BottomSheetController来控制显示和隐藏,并且可以自定义内容布局。BottomSheet组件适用于需要从底部弹出的轻量级交互场景,如选择器、菜单或信息展示。

在HarmonyOS鸿蒙Next中,底部弹出的弹窗通常使用BottomSheet组件。BottomSheet是一个从屏幕底部滑出的面板,可以展示更多内容或操作选项。它适用于需要用户进行选择或执行操作的场景,具有良好的用户体验和交互效果。可以通过showBottomSheet方法来显示,并通过hideBottomSheet方法来隐藏。

回到顶部