HarmonyOS NEXT星河版之模拟图片选择器(下)---使用CustomDialog展示图片

一、目标

在这里插入图片描述
在这里插入图片描述

二、开撸

2.1 自定义弹窗

@CustomDialog
export struct SinglePreviewDialog {
  // 弹窗控制器 must
  controller: CustomDialogController
  // 展示图片URL
  imgUrl: ResourceStr = ''

  build() {
    Column() {
      Image(this.imgUrl)
        .width('100%')
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor(Color.Black)
    .onClick(() => {
      // 关闭弹窗
      this.controller.close()
    })
  }
}

2.2 主页面事件处理

声明变量,记录当前选择图片地址:

selectImgUrl: ResourceStr = ''

声明自定义Dialog对象:

singlePreviewDialog: CustomDialogController = new CustomDialogController({
  builder: SinglePreviewDialog({
    imgUrl: this.selectImgUrl
  }),
  customStyle: true //使用自定义Dialog的样式
})

图片注册点击事件:

Image(item.imgUrl)
    .aspectRatio(1)
    .onClick(() => {
      this.selectImgUrl = item.imgUrl
      this.singlePreviewDialog.open()
    })

2.3 主页面完整代码

import { PhotoAlbumView } from './components/PhotoAlbumView';
import { SelectImageItem } from './models';
import { promptAction } from '@kit.ArkUI';
import { SinglePreviewDialog } from './dialog/SinglePreviewDialog';

@Entry
@Component
struct PhotoAlbumDemoPage {
  @State message: string = 'Hello World';
  @State showPhotoAlbum: boolean = false
  @State selectImgList: SelectImageItem[] = []
  // 当前选中图片
  selectImgUrl: ResourceStr = ''
  // 单图预览dialog
  singlePreviewDialog: CustomDialogController = new CustomDialogController({
    builder: SinglePreviewDialog({
      imgUrl: this.selectImgUrl
    }),
    customStyle: true //使用自定义Dialog的样式
  })

  build() {
    Column() {
      Button('打开相册')
        .onClick(() => {
          this.showPhotoAlbum = true
        })
      Text('已选择图片:')
        .width('100%')
        .textAlign(TextAlign.Start)
      if (this.selectImgList && this.selectImgList.length) {
        Grid() {
          ForEach(this.selectImgList, (item: SelectImageItem) => {
            GridItem() {
              Stack() {
                Image(item.imgUrl)
                  .aspectRatio(1)
                  .onClick(() => {
                    this.selectImgUrl = item.imgUrl
                    this.singlePreviewDialog.open()
                  })
              }
            }
          }, (item: SelectImageItem) => JSON.stringify(item))
        }
        .columnsTemplate('1fr 1fr 1fr')
        .columnsGap(5)
        .rowsGap(5)
        .padding(10)
        .layoutWeight(1)
      }
      if (this.showPhotoAlbum) {
        PhotoAlbumView({
          maxNumber: 5,
          cancel: () => {
            this.showPhotoAlbum = false
          },
          confirm: (selectImgList: SelectImageItem[]) => {
            this.showPhotoAlbum = false
            this.selectImgList = [...this.selectImgList, ...selectImgList]
          }
        })
      }
    }
    .width('100%')
    .height('100%')
  }
}

三、小结

  • open 创建弹层组件-显示-会有动画的弹出
  • close 销毁组件-推出-会有动画的退出
  • open/close会创建和销毁组件,不存在缓存现象,里面的参数实际上没有任何必要使用@State修饰符
  • customStyle是否允许自定义的样式设置,因为默认的弹层是有一些定制的样式的
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值