ArkTS task statistics page

We continue to learn to use ArkTS to implement the statistical function of a task progress.

 

1. Public components and styles


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: '#1f0000',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[] = []

  build() {
      Column({space:10}) {

      }
      .width('100%')
      .height('100%')
      .backgroundColor('#f1f2f3')
  }
}

The entire demo is mainly divided into three parts, the task progress card at the top, the new button in the middle, and the task progress list below. Let's do it step by step 

2. Production of task progress cards

1.Typesetting

We use line labels wrapped with two text labels to display numbers and use justifyContent(FlexAlign.SpaceEvenly) for alignment processing.

        Row(){
          Text("任务进度")
            .fontSize(30)
            .fontWeight(FontWeight.Bold)
          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. Task bar component

When we use the progress bar component, we can check the official documentation of ArkUI.

Document Center

The documentation includes how to use components and the types of values ​​they pass.

 

3. Overlay containers

We'll notice that the numbers are not embedded in our taskbar component. We need to use the stacking container Stack. 

For specific documentation, please refer to the ArkTS official documentation. Just put the containers to be stacked on Stack({

}) in it.

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

This completes our task progress card. 

2. Create new task buttons

        // 2.新增任务按钮
        Button('新增任务')
          .width(200)
          .onClick(()=>{
            // 1.新增任务
            this.tasks.push(new Task())
            // 2.更新任务数组
            this.totalTask = this.tasks.length
          })

3. Creation of task list

When we make a task list, we need to use a check box, which can be used by referring to the documentation.

Document Center

We use foreach to traverse the array to complete the implementation of the task list, use checkbox to complete the logic of the check box, and use the array method filter to filter the selected ones, that is, the number of completions to change the status of the task bar component. 

Improve code reusability by encapsulating a function that updates task progress

  // 通过过滤方法 更新已完成的任务数量
  handleTaskChange(){
    // 更新任务总数量
    this.totalTask = this.tasks.length
    // 已经完成的任务数量
    this.finishTask = this.tasks.filter(item => item.finished).length
  }
        // 3.卡片列表
        ForEach(
          this.tasks,
          (item: Task,index)=>{
            Row(){
              Text(item.name)
                .fontSize(20)
              Checkbox()
                .select(item.finished)
                .onChange(val=>{
                  item.finished = val
                  // 通过过滤方法 更新已完成的任务数量
                  this.finishTask = this.tasks.filter(item => item.finished).length
                  this.handleTaskChange()
                })
            }
            .card()
            .justifyContent(FlexAlign.SpaceBetween)
          }
        )

Our preliminary style of task progress management is completed.

 

 4. Implementation of sliding delete operation

Our function requires a button that slides left to delete tasks, and the bottom should be scrollable. We transform our task list into a List to complete this operation.

        // 3.卡片列表
        List({space:10}){
          ForEach(
            this.tasks,
            (item: Task,index)=>{
              ListItem(){
                Row(){
                  Text(item.name)
                    .fontSize(20)
                  Checkbox()
                    .select(item.finished)
                    .onChange(val=>{
                      item.finished = val
                      // 通过过滤方法 更新已完成的任务数量
                      this.handleTaskChange()
                    })
                }
                .card()
                .justifyContent(FlexAlign.SpaceBetween)
              }
            }
          )
        }
        .width('100%')
        .layoutWeight(1)
        .alignListItem(ListItemAlign.Center)

Using list and using the layoutWeight attribute to get all the remaining heights can ensure that our height is adaptive and can be pulled down, which solves the problem of too many invisible tasks.

 A button appears when you slide it to the left. List has an attribute swipeAction that can be used directly. You can check the document for how to use it.


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: '#1f0000',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(){
    // 更新任务总数量
    this.totalTask = this.tasks.length
    // 已经完成的任务数量
    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
            })
              .width(100)
            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.totalTask = this.tasks.length
          })

        // 3.卡片列表
        List({space:10}){
          ForEach(
            this.tasks,
            (item: Task,index)=>{
              ListItem(){
                Row(){
                  Text(item.name)
                    .fontSize(20)
                  Checkbox()
                    .select(item.finished)
                    .onChange(val=>{
                      item.finished = val
                      // 通过过滤方法 更新已完成的任务数量
                      this.handleTaskChange()
                    })
                }
                .card()
                .justifyContent(FlexAlign.SpaceBetween)
              }
              .swipeAction({end:this.DeleteButton(index)})
            }
          )
        }
        .width('100%')
        .layoutWeight(1)
        .alignListItem(ListItemAlign.Center)
      }
      .width('100%')
      .height('100%')
      .backgroundColor('#f1f2f3')
  }
  @Builder DeleteButton(index: number){
      Button("删除")
        .onClick(()=>{
          this.tasks.splice(index,1)
          this.handleTaskChange()
        })
  }
}

At this point, we have finished studying the task statistics case. 

 

Guess you like

Origin blog.csdn.net/a_strong_pig/article/details/134993077
Recommended