HarmonyOS学习4 --- 创建一个页面

1、声明式UI语法

复制代码
@Entry
@Component
struct My_page {
  @State isLogin: boolean = false

  build() {
    Row() {
      Image(this.isLogin ? $r('app.media.icon_leon') : $r('app.media.icon'))
        .height(60)
        .width(60)
        .onClick(() => {
          this.isLogin = !this.isLogin
        })

      Text(this.isLogin ? $r('app.string.string_to_unlogin') : $r('app.string.string_to_unlogin'))
        .fontWeight(FontWeight.Bold)
        .fontSize(20)
        .fontColor(this.isLogin ? Color.Orange : Color.Black)
        .margin({ "top": "0.00vp", "right": "0.00vp", "bottom": "0.00vp", "left": "10.00vp" })
    }
    .width('100%')
    .margin({ "top": "20.00vp", "right": "10.00vp", "bottom": "0.00vp", "left": "10.00vp" })
  }
}

2、自定义组件

2.1、自定义组件生命周期

注:页面,即由 @Entry 修饰的自定义组件

注:自定义组件,即由 @Component 修饰的UI单元

1)aboutToAppear()

2)onPageShow()

注:仅在被 @Entry 修饰的组件中生效

3)onBackPress()

注:仅在被 @Entry 修饰的组件中生效

4)onPageHide()

注:仅在被 @Entry 修饰的组件中生效

5)aboutToDisappear()

2.2、案例

注:@State 可建立起视图与状态之间的绑定关系

  • ToItem

    @Component
    export default struct ToItem {
    public message?: string
    @State isComplete: boolean = false

    复制代码
    @Builder leonSelectLabel(icon: Resource) {
      Image(icon)
        .objectFit(ImageFit.Contain)
        .width('28vp')
        .width('28vp')
        .margin('20vp')
    
    }
    
    build() {
      Row() {
        if (this.isComplete) {
          this.leonSelectLabel($r('app.media.ic_ok'))
        } else {
          this.leonSelectLabel($r('app.media.ic_default'))
        }
        Text(this.message??'无')
          .fontSize('20fp')
          .fontWeight(FontWeight.Bold)
          .decoration({ type: this.isComplete ? TextDecorationType.LineThrough : TextDecorationType.None })
          .fontColor(this.isComplete ? Color.Gray : Color.Black)
      }
      .width('100%')
      .borderRadius(6)
      .backgroundColor(Color.White)
      .onClick(() => {
        this.isComplete = !this.isComplete
      })
    }

    }

复制代码
ToDoListPage
复制代码
import ToDoItem from '../view/ToDoItem'
import DataModel from '../viewmodel/DataModel'

@Entry
@Component
struct ToDoListPage {
  private todoLists: string[]

  aboutToAppear() {
    this.todoLists = DataModel.getData()
  }

  build() {

    Column({ space: 16 }) {

      Text("待办任务列表")
        .fontWeight(FontWeight.Bold)
        .fontSize(30)
        .fontColor(Color.Blue)
        .margin({ top: 10, left: 10 })


      ForEach(this.todoLists, (item: string) => {
        ToDoItem({ message: item }).width('90%')
      }, (item: string) => JSON.stringify(item))

    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.page_background'))

  }
}
复制代码
DataModel
复制代码
export class DataModel {
  public tasks: string[] = ["早起晨练", "准备早饭", "学习编程", "阅读名著", "自由活动"]

  getData() {
    return this.tasks
  }
}


export default new DataModel()
相关推荐
梁山好汉(Ls_man)17 小时前
鸿蒙_自定义组件包含多个引用自定义构建函数@BuilderParam时的用法
华为·harmonyos·鸿蒙·arkui
UnicornDev21 小时前
【HarmonyOS 6】鸿蒙原生应用智能体接入
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
梦想不只是梦与想21 小时前
鸿蒙中 PhotoViewPicker:选择图片或视频
harmonyos·鸿蒙·photoviewpicker
星释1 天前
鸿蒙Flutter实战:29.优先使用联合插件开发鸿蒙化插件
flutter·华为·harmonyos·鸿蒙
加农炮手Jinx1 天前
Flutter 三方库 better_commit 的鸿蒙化适配指南 - 实现具备语义化提交规范与自动化交互的 Git 工作流插件、支持端侧版本工程的高效规范化审计实战
flutter·harmonyos·鸿蒙·openharmony·better_commit
2301_822703202 天前
渐变壁纸生成:基于鸿蒙Flutter的跨平台壁纸创建工具
flutter·华为·harmonyos·鸿蒙
2301_822703202 天前
开源鸿蒙跨平台Flutter开发:幼儿疫苗全生命周期追踪系统:基于 Flutter 的免疫接种档案与状态机设计
算法·flutter·华为·开源·harmonyos·鸿蒙
2301_822703202 天前
鸿蒙flutter三方库实战——教育与学习平台:Flutter Markdown
学习·算法·flutter·华为·harmonyos·鸿蒙
2301_822703202 天前
开源鸿蒙跨平台Flutter开发:蛋白质序列特征提取:氨基酸组成与理化性质计算
flutter·华为·开源·harmonyos·鸿蒙
钛态2 天前
Flutter 三方库 ethereum_addresses 的鸿蒙化适配指南 - 掌控区块链地址资产、精密校验治理实战、鸿蒙级 Web3 专家
flutter·harmonyos·鸿蒙·openharmony·ethereum_addresses