DEV Community

wei chang
wei chang

Posted on • Edited on

在鸿蒙Next中开发一个底部选项弹窗

今天大暑,酷暑难耐,蚊虫叮咬,幽蓝君依然在出差地饱受煎熬,打工人难呐。

今天和大家分享一个比较常见的底部选项弹窗,效果图如下:

Image description

这个弹窗基于系统的弹窗组件CustomDialogController开发,在它的builder参数中我们可以传入一些自定义的样式,所以我自定义一个选项列表:

@CustomDialog
struct AlertSheet {
  controller?: CustomDialogController
   @Prop titles:string[]
  itemClick:(str:string)=>void=()=>{}

  build() {
    Column() {
      ForEach(this.titles,(str:string,index)=>{
        Row(){
          Text(str)
            .fontColor('rgb(0,122,255)')
            .fontSize(17)
        }
        .alignItems(VerticalAlign.Center)
        .justifyContent(FlexAlign.Center)
        .width('100%')
        .height(50)
        .border({width:{bottom:0.5},color:{bottom:'rgb(216,216,216)'},style:{bottom:BorderStyle.Solid}})
        .onClick(()=>{
          this.controller?.close()
          this.itemClick(str)
        })
      })
      Row(){
        Text('取消')
          .fontColor('rgb(0,122,255)')
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.Center)
      .alignItems(VerticalAlign.Center)
      .width('100%')
      .height(80)
      .onClick(()=>{
        this.controller?.close()
      })
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

引用的时候传入选项内容和回调事件就可以了:

dialogController: CustomDialogController = new CustomDialogController({
    builder: AlertSheet({titles:['选项1','选项2','选项3','选项4'],itemClick:(str)=>{
      console.log('点击了:' + str)
    }}),
    alignment: DialogAlignment.Bottom,
  })
Enter fullscreen mode Exit fullscreen mode

Image of Quadratic

Python + AI + Spreadsheet

Chat with your data and get insights in seconds with the all-in-one spreadsheet that connects to your data, supports code natively, and has built-in AI.

Try Quadratic free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay