HarmonyOS 鸿蒙Next bindSheet

HarmonyOS 鸿蒙Next bindSheet

官方有打算把bindSheet 设计成 panel 一样从组件底部弹出吗?

5 回复

三段式拖动页面:

@Entry
@Component
struct Index {
  private controller: TabsController = new TabsController()
  @State currentIndex: number = 0;
  @Builder TabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
    Column() {
      Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
        .size({ width: 25, height: 25 })
      Text(title)
        .fontColor(this.currentIndex === targetIndex ? '#ff1244' : '#6B6B6B')
    }
    .width('100%')
    .height(50)
    .justifyContent(FlexAlign.Center)
    .onClick(() => {
      this.currentIndex = targetIndex;
      this.controller.changeIndex(this.currentIndex);
    })
  }
  aboutToAppear(): void {
  }
  build() {
    Column() {
      Tabs({ barPosition: BarPosition.End,controller: this.controller,index:this.currentIndex}) {
        TabContent() {
          Column(){
          }.backgroundColor("#FFC0CB").height('100%').width('100%')
          .overlay(this.getPanel(), {
            align: Alignment.Bottom,
          })
        }
        .backgroundColor("#D1D1D1")
        .tabBar(this.TabBuilder('tab1',0,$r('app.media.startIcon'),$r('app.media.startIcon')))
        TabContent() {
          Text('测试')
        }
        .backgroundColor("#D1D1D1")
        .tabBar(this.TabBuilder('tab2',1,$r('app.media.startIcon'),$r('app.media.startIcon')))
      }
    }
    .width('100%')
  }
  @Builder getPanel(){
    Column(){
      Column(){
      }.width('100%').height('10%').backgroundColor('white')
      Panel(true) { // 展示
        Column() {
          Text('弹框内容')
        }
      }.height('90%')
      .type(PanelType.Foldable)
      .mode(PanelMode.Full)
      .dragBar(true) // 默认开启
      .showCloseIcon(false) // 显示关闭图标
      .onChange((width: number, height: number, mode: PanelMode) => {
        console.info(`width:${width},height:${height},mode:${mode}`)
      })
      .onHeightChange((height)=>{
      })
    }.height('100%')
  }
}

更多关于HarmonyOS 鸿蒙Next bindSheet的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


可以先使用别的实现该功能,看下我写的demo:

Index.ets
// Index.ets
import { CustomPopDialog } from './CustomPopDialog'
import {DialogController} from './DialogController'
@Entry
@Component
struct LoginPage {
  controller: DialogController = new DialogController()
  build() {
    Column() {
      CustomPopDialog({
        controller: this.controller,
        dialogHeight: '70%'
      }) {
        Column() {
          Text('快速登录')
            .fontWeight(FontWeight.Bold)
            .onClick(() => {
              this.controller.openDialog()
            });
        }
        .width('100%').height('90%').constraintSize({maxHeight:'90%'})
      }
      Column(){
        Button('快速登陆')
      }.height('10%').width('100%').onClick(() => {
        this.controller.openDialog()
      }).backgroundColor(Color.Red);
    }.justifyContent(FlexAlign.SpaceBetween)
    .width('100%')
    .height('100%')
  }
}
CustomPopDialog.ets
// CustomPopDialog.ets
import { DialogController } from './DialogController';
@Component
export struct CustomPopDialog {
  private controller: DialogController | undefined;
  @Prop dialogWidth: Length = '100%' // 弹框的宽度度
  @Prop dialogHeight: Length = 400 // 弹框的高度
  @Prop radius: number = 16 // 圆角
  @Prop maskColor: ResourceColor = '#4d000000' // 背景蒙层颜色
  @State title: string = '帐号密码登录' // 短信验证码登录
  @State subTitle: string = '登录帐号以使用更多服务' // 未注册手机号验证通过后将自动注册
  @State openState: number = 0;
  @BuilderParam slotPage: () => void = this.doNothingBuilder;
  @Builder
  doNothingBuilder() {
  }
  aboutToAppear() {
    if (this.controller) {
      this.controller.dialogCallback = (state: number) => {
        if (this.openState == state) {
          return;
        }
        this.openState = state;
      }
    }
  }
  @Builder
  header() {
    Row() {
      Image($r('app.media.startIcon'))
        .width(26).height(26)
        .onClick(() => {
          this.controller?.closeDialog()
        })
    }
    .width('100%')
    .padding(14)
    .justifyContent(FlexAlign.End)
  }
  @Builder
  body() {
    Column() {
      Text(this.title)
        .fontSize(24)
        .fontWeight(FontWeight.Medium)
        .fontColor('#182431')
      Text(this.subTitle)
        .fontSize(12)
        .fontColor('#99182431')
        .margin({ bottom: 30, top: 8 })
    }
    .width('100%')
    .padding({ left: 14, right: 14 })
    .alignItems(HorizontalAlign.Start)
  }
  build() {
    Stack({ alignContent: Alignment.Center }) {
      this.slotPage()
      if (this.openState == 1) {
        // mask蒙层
        Column() {
        }
        .width('100%')
        .height('100%')
        .backgroundColor(this.maskColor)
        // 具体内容
        Column() {
          Column() {
            this.header() // 标题
            this.body() // 内容
          }
          .backgroundColor(Color.White)
          .width(this.dialogWidth)
          .height(this.dialogHeight)
          .borderRadius({ topLeft: this.radius, topRight: this.radius })
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.End)
        .transition(TransitionEffect.OPACITY.animation({ duration: 200 })
          .combine(TransitionEffect.translate({ y: 100 })))
      }
    }
    .width('100%')
    .height('90%')
  }
}
DialogController.ets
// DialogController.ets
export class DialogController {
  public dialogCallback: (state: number) => void = (state: number) => {
  };
  openDialog() {
    this.dialogCallback?.(1)
  }
  closeDialog() {
    this.dialogCallback?.(0)
  }
}

好的好的,感谢,这种确实可以实现没有拖动效果的页面。 如果是类似地图首页那种三段式拖动的页面,还得自己处理手势事件,好麻烦

在HarmonyOS(鸿蒙)系统中,bindSheet 并不是一个标准的API或组件名称,因此直接回答关于它的使用或功能可能并不准确。然而,基于你的提问,我假设你可能是在探讨如何在鸿蒙系统中绑定或管理某种表单(Sheet)界面或数据。

在鸿蒙开发中,管理表单或数据通常涉及到使用ArkUI(使用TS/JS扩展的声明式UI框架)或eTS(Enhanced TypeScript)来创建和绑定UI组件。对于表单数据的绑定,你可能会使用数据绑定机制,如双向绑定(v-model)或单向绑定({{ }}),来确保UI组件与数据模型的同步。

如果你是在尝试绑定一个特定的Sheet组件(假设为某种自定义或第三方组件),你应该参考该组件的官方文档或示例代码来了解如何正确绑定和使用它。

如果bindSheet是你遇到的一个特定方法或属性,并且不是来自鸿蒙的标准库,那么它可能是某个特定库或框架的一部分。在这种情况下,你应该查找该库或框架的文档以获取更详细的信息。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部