HarmonyOS Next应用开发实战——底部弹框组件的实现(part2)

3. 排序方式选择对话框

点击"综合排序"可弹出排序方式选择对话框,用户可以选择不同的排序方式。

typescript 复制代码
Row(){
  Text('综合排序').fontSize(CommonConstants.M_FONT_SIZE)
  Image($r('app.media.ic_arrow_drop_down'))
    .width(CommonConstants.PIC_WIDTH).height(CommonConstants.PIC_HEIGHT)
}
.justifyContent(FlexAlign.Start)
.alignItems(VerticalAlign.Center)
.onClick(() => {
  this.dialogController.open()
})

@CustomDialog
struct CustomDialogExample {
  cancel?: () => void
  controller: CustomDialogController

  build() {
    Column() {
      Text('排序方式').fontSize(CommonConstants.L_FONT_SIZE).margin({ top: 0, bottom: 20 })
      Column() {
        Text('综合排序').textDialogStyle()
        Text('距离优先').textDialogStyle()
        Text('价格优先').textDialogStyle().borderWidth(0)
      }
      .justifyContent(FlexAlign.Start)
      .alignItems(HorizontalAlign.Start)
      .width(CommonConstants.COLUMN_WIDTH)

      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('取消')
          .onClick(() => {
            this.controller.close()
            if (this.cancel) {
              this.cancel()
            }
          })
          .fontColor($r('app.color.main_color')).fontSize(CommonConstants.S_FONT_SIZE)
          .backgroundColor(Color.Transparent)
      }.margin({ bottom: CommonConstants.MAIN_PADDING })
    }
    .alignItems(HorizontalAlign.Start)
    .padding({
      top: CommonConstants.MAIN_PADDING * 2,
      right: CommonConstants.MAIN_PADDING * 2,
      left: CommonConstants.MAIN_PADDING * 2
    })
    .width(CommonConstants.COLUMN_WIDTH)
  }
}
4. 站点列表展示

使用ListForEach组件展示站点列表,显示站点名称、距离、标签、充电桩类型和价格等信息。

typescript 复制代码
List({ space: CommonConstants.PUBLIC_SPACE }) {
  ForEach(this.StationList, (item: StationInfo, index: number) => {
    ListItem() {
      Column({ space: CommonConstants.PUBLIC_SPACE }) {
        Flex({ justifyContent: FlexAlign.SpaceBetween }) {
          Text(item.name).fontSize(CommonConstants.M_FONT_SIZE)
          Row({ space: CommonConstants.PUBLIC_SPACE / 2 }) {
            Image($r('app.media.ic_collect'))
              .width(CommonConstants.PIC_WIDTH / 2)
              .height(CommonConstants.PIC_HEIGHT / 2)
            Text(`${item.distance}km`).fontSize(CommonConstants.S_FONT_SIZE).fontColor('#a9a9a9')
          }
        }

        Row() {
          ForEach(item.tag as string[], (itemTag: string, index: number) => {
            Text(`${itemTag}`)
              .fontSize(CommonConstants.S_FONT_SIZE)
              .margin({ right: CommonConstants.PUBLIC_SPACE / 2 })
              .padding({ top: '2vp', right: '5vp', bottom: '2vp', left: '5vp' })
              .borderRadius(4)
              .backgroundColor('#e2e2e2')
          })
        }.width(CommonConstants.COLUMN_WIDTH)

        Flex({ justifyContent: FlexAlign.SpaceBetween }) {
          Row() {
            Row() {
              Text(`直流`)
                .fontSize(CommonConstants.S_FONT_SIZE)
                .backgroundColor('#ceffbc')
                .fontColor('#1ed013')
                .padding('2vp')
                .borderRadius(4)
              Text(`${item.direct}`).fontSize(CommonConstants.M_FONT_SIZE).fontWeight(600)
            }
            .margin({ right: '20vp' })

            Row() {
              Text(`交流`)
                .fontSize(CommonConstants.S_FONT_SIZE)
                .backgroundColor('#c3caf4')
                .fontColor('#0269da')
                .padding('2vp')
                .borderRadius(4)
              Text(`${item.alternating}`)
            }
          }

          Row() {
            Text(`¥${item.price}`)
            Text('/度起')
              .fontSize(CommonConstants.S_FONT_SIZE)
              .fontColor('#a9a9a9')
          }
          .alignItems(VerticalAlign.Bottom)
        }
      }
      .backgroundColor(Color.White)
      .borderRadius(CommonConstants.BORDER_RADIUS)
      .padding(CommonConstants.MAIN_PADDING)
    }
  })
}
.scrollBar(BarState.Off)
.layoutWeight(1)
5. 底部弹框显示与隐藏控制

通过点击"重新推荐"按钮控制底部弹框的显示与隐藏。

typescript 复制代码
build() {
  Column() {
    Button('重新推荐')
      .onClick(() => {
        this.isShow = true
      })
      .fontSize(CommonConstants.L_FONT_SIZE)
      .bindSheet($$this.isShow, this.myBuilder(), {
        detents: [SheetSize.MEDIUM, SheetSize.LARGE, 200],
        showClose: false,
        preferType: SheetType.CENTER,
        enableOutsideInteractive: true,
        shouldDismiss: ((sheetDismiss: SheetDismiss) => {
          sheetDismiss.dismiss()
        })
      })
  }
  .justifyContent(FlexAlign.Center)
}

通过以上核心功能的实现,开发者可以在HarmonyOS Next应用中创建一个功能完善的底部弹框组件。

相关推荐
鸿蒙布道师10 小时前
鸿蒙NEXT开发Base64工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
The 旺11 小时前
《HarmonyOS Next开发实战:从零构建响应式Todo应用的基石》
华为·harmonyos
Industio_触觉智能11 小时前
鸿蒙北向开发OpenHarmony5.0 DevEco Studio开发工具安装与配置
harmonyos·鸿蒙系统·openharmony·开源鸿蒙·鸿蒙开发·嵌入式开发板
秋叶先生_16 小时前
HarmonyOS NEXT——【鸿蒙监听网络状态变化】
华为·harmonyos·鸿蒙
东林知识库16 小时前
鸿蒙NEXT小游戏开发:围住神经猫
harmonyos
zacksleo16 小时前
鸿蒙Flutter开发故事:不,你不需要鸿蒙化
flutter·harmonyos
别说我什么都不会18 小时前
OpenHarmony解读之设备认证:sts协议-客户端发起sts end请求
物联网·嵌入式·harmonyos
悬空八只脚21 小时前
React-Native开发鸿蒙NEXT-本地与沙盒加载bundle
harmonyos
鸿蒙布道师21 小时前
鸿蒙NEXT开发日志工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
90后的晨仔1 天前
HarmonyOS的页面生命周期 和 组件生命周期
harmonyos