ArkUI-12-状态管理02

@Pro @Link @Provide @Consume

代码:

复制代码
//任务类
class Task{
  static id: number=1
  //任务名称
  name:string = `任务${Task.id++}`
  //任务状态:是否完成
  finished:boolean=false
}
//统一的卡片样式
@Styles function card(){
  .width('95%')
  .padding(20)
  .backgroundColor(Color.White)
  .borderRadius(15)
  .shadow({radius:6,color:'#1F000000',offsetX:2,offsetY:4})
}

//任务完成样式
@Extend(Text)  function finishedTask(){
  .decoration({type:TextDecorationType.LineThrough})
  .fontColor('#B1B2B1')
}

@Entry
@Component
struct PropPage{
  //总任务数量
  @State totalTask:number=0
  //已经完成任务数量
  @State finishTask:number=0
  //任务数组
  @State tasks: Task[] = []

  handleTaskChange(){
    //1。更新任务数量
    this.totalTask=this.tasks.length
    //2.更新当前的状态
    this.finishTask = this.tasks.filter(item=> item.finished).length

  }
  
  build(){
    Column({space:10}){
      //1.任务进度卡片
      Row(){
        Text('任务进度:')
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
        Stack(){
          Progress({
            value:this.finishTask,
            total:this.totalTask,
            type:ProgressType.Ring
          })
          Row(){
            Text(this.finishTask.toString())
              .fontSize(24)
              .fontColor('#36D')
            Text(' / ' + this.totalTask.toString())
              .fontSize(24)
          }
        }

      }
      .card()
      .margin({top:20,bottom:10})
      .justifyContent(FlexAlign.SpaceEvenly)
      //2.新增任务按钮
      Button('新建任务')
        .width(200)
        .onClick(()=>{
          //1.新增任务数据
          this.tasks.push(new Task())
          //2。更新任务数量
          this.handleTaskChange()
        })

      //3.任务的列表
     List({space:10}){
       ForEach(
         this.tasks,
         (item:Task,index)=>{
           ListItem(){
             Row(){
               Text(item.name)
                 .fontSize(20)
               Checkbox()
                 .select(item.finished)
                 .onChange(val=>{
                   //1.更新当前的状态
                   item.finished = val
                   //2.更新当前的状态
                   this.handleTaskChange()

                 })
             }
             .card()
             .justifyContent(FlexAlign.SpaceBetween)
           }.swipeAction({end:this.DeleteButton(index)})//滑动功能

       })
     }
     .width('100%')
     .layoutWeight(1)
     .alignListItem(ListItemAlign.Center)


    }.width('100%')
    .height('100%')
    .backgroundColor('#1F2F3')
  }
  @Builder DeleteButton(index:number){
        Button(){//删除按钮
          Image($r('app.media.delete'))
            .fillColor(Color.White)
            .width(20)
        }
     .width(40)
    .height(40)
    .type(ButtonType.Circle)
    .backgroundColor(Color.Red)
    .margin(10)
    .onClick(()=>{
      this.tasks.splice(index,1)//删除当前这条
      this.handleTaskChange()//更新结果
    })
  }
}
相关推荐
默 语7 分钟前
HarmonyOS7基础属性全解-组件标识id
harmonyos
默 语13 分钟前
HarmonyOS7基础属性全解(八):隐私遮罩 obscured——屏幕上看得清清楚楚,录屏里只剩占位图
wpf·harmonyos
默 语20 分钟前
HarmonyOS7基础属性全解(五):背景设置——颜色垫底、图片其上、模糊封顶
harmonyos
默 语23 分钟前
HarmonyOS7基础属性全解(七):Z序控制 zIndex——谁浮在上面,代码说了算
harmonyos
ovenslove4 小时前
权限申请:为什么你的相机打不开
harmonyos·arkts
NJPJI_Yang4 小时前
鸿蒙操作系统使用 Media Library Kit 实现媒体库资源保存与查询实战(本文已在AtomGit仓库开源)
华为·开源·harmonyos
OH_TPC4 小时前
@ohos/grpc 源码解读(二):四种 RPC 模式的流状态机——EOF、DEFERRED 与 wakeupPipe 的分工
harmonyos
行者-全栈开发8 小时前
【鸿蒙心迹】性能常态化检测——内存 / CPU / 帧率三件套怎么建基线、设门禁(HarmonyOS 7.x)
harmonyos·内存泄漏·cpu占用·ci门禁·性能检测·帧率监控·性能基线
NJPJI_Yang11 小时前
鸿蒙操作系统使用 Image Kit 实现图片解码编辑与压缩保存实战(本文已在AtomGit仓库开源)
华为·开源·harmonyos
程序猿追11 小时前
HarmonyOS 7 剪贴板读写,同一份内容怎样带上纯文本和 HTML
华为·html·harmonyos