ArkTS+UI写的任务列表

只有增删的功能,查询功能不全

把所有的view和model都写在了一个ets文件中,涉及到了父子组件数据双向传递@prop,将父组件的方法传递给子组件的方法并使用.bind(this)将当前this传递给子组件的方法,以及自定义弹窗组件@ComtumeDialog和它的CustomDialogController类,以及Progress组件(任务进度组件)
此外,数据并没有持久化,所以刷新之后,数据变回默认状态
效果
在这里插入图片描述
代码

class Task{
  name:string
  id:number
  finished:boolean

  constructor(id:number,name:string,finished:boolean) {
    this.id = id
    this.name = name
    this.finished = finished
  }
}

@Entry
@Component
struct TaskListPage {
  title:string= '海草TASK'
  brandName:string = '@海草笔记'
  @State tasks:Array<Task> = [new Task(1,'name',false),new Task(1,'name',false)]
  // 总任务数量
  @State totalTask: number = this.tasks.length
  @State finishTask: number = this.tasks.filter(item=>item.finished).length


  dialogController: CustomDialogController = new CustomDialogController({
    builder: TaskInfoDialog({onTaskConfirm:this.handleAddTask.bind(this),handleTaskChange:this.handleTaskChange.bind(this)}),
  })
  //删除按钮和删除功能
  @Builder deleteButton(index:number){
    Button(){
      Image($r('app.media.icon'))
        .width(20)

    }
    .width(40)
    .height(60)
    .onClick(()=>{
      this.tasks = this.tasks.slice(0, index).concat(this.tasks.slice(index + 1));

    })
  }
  @Builder addButton(){
  Button('新增')
    .onClick(()=>{
      // 打开新增表单对话框
      this.dialogController.open()

    })
  }
  handleAddTask(name: string) {
  let task = new Task(1,name,false)
    console.log(`${task.name}`)
    // console.log(`${this.tasks}`)
  this.tasks.push(task)
    this.dialogController.close()
  }
  handleTaskChange(){
    // 1.更新任务总数量
    this.totalTask = this.tasks.length
    // 2.更新已完成任务数量
    this.finishTask = this.tasks.filter(item => item.finished).length
    console.log(`total:${this.totalTask},finished:${this.finishTask}`)
  }

  build() {
    Column(){
      //头部
      HeaderCom({title:this.title})
      TaskStatics({totalTask:this.totalTask,finishTask:this.finishTask})
      //任务列表
      List({space:6}){
        ForEach(this.tasks,(item:Task,index)=>{
          ListItem(){
            MiddleItemCom({task:item,handleTaskChange:this.handleTaskChange.bind(this)})
            // MiddleItemCom()
          }
          .swipeAction({end:this.deleteButton(index)})
        })
      }
      .alignListItem(ListItemAlign.Center)
      .width('100%')
      .height('60%')
      .backgroundColor(Color.Green)
      //新增按钮
      this.addButton()
      //品牌
      BottomCom({brandName:this.brandName})

    }
    .height('100%')
    .width('100%')

  }
}

//头部自定义组件
@Component
struct HeaderCom{
  title:string = ''

  build() {
    Row(){
      Text(this.title)
        .fontSize('30')
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height(50)
    .backgroundColor('#ffa75809')
  }
}
//中间任务列表组件
@Component
struct MiddleItemCom{
  @State task:Task = new Task(1,'name',true)
  fontSize:number = 20
  myBorderRadius:number = 5
  handleTaskChange:()=>void = ()=>{}
  build() {
    Row(){

      //用CheckBox来展示是否已经完成
      if(this.task.finished){
        //序号+任务名
        Text(this.task.name)
          .fontSize(this.fontSize)
          .finishedStyle()
      }else {
        Text(this.task.name)
          .fontSize(this.fontSize)
      }
      Blank()
      Checkbox()
        .select(this.task.finished)
        .width(20)
        .height(20)
        .onChange((value)=>{
          this.task.finished = value
          this.handleTaskChange()
        })
    }

    .backgroundColor(Color.White)
    .height(30)
    .width('95%')
    .borderRadius(this.myBorderRadius)
    .margin(5)

  }
}
//底部组件
@Component
struct BottomCom{
  brandName:string = ''
  build() {
    Row(){
      Text(this.brandName)
        .fontSize('10')
    }
    .width('100%')
    .height(20)
    .justifyContent(FlexAlign.Center)
  }
}

@Component
struct Com{
  build(){

  }
}

@CustomDialog
struct TaskInfoDialog{
  @Builder con(){}
  name:string = ''
  onTaskConfirm : (name: string) => void  = (name:string)=> {};
  handleTaskChange:()=>void = ()=>{}
  dialogController: CustomDialogController = new CustomDialogController({builder:Com})
  build() {
    Column({space:20}){
      TextInput({placeholder: '输入任务名'})
        .onChange(val => this.name = val)
      Row(){
        Button('确定')
          .onClick(() => {
            if (this.onTaskConfirm) {
              // console.log(`${this.name}`)
              this.onTaskConfirm(this.name)
              //更新任务进度
              this.handleTaskChange()
            }else {
              console.log('testTag','160line error')
            }

          })
        Button('取消')
          .backgroundColor(Color.Grey)
          .onClick(() => {
            this.dialogController.close()
          })
      }
      .width('100%')
      .justifyContent(FlexAlign.SpaceEvenly)
    }
    .width('100%')
    .padding(20)
    }

}

@Component
struct TaskStatics{
  @Prop totalTask: number
  @Prop finishTask: number
  build() {
    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: 5, bottom: 10})
    .justifyContent(FlexAlign.SpaceEvenly)
  }
}

@Extend(Text)
function finishedStyle(){
  .fontColor('#ccc')
  .decoration({
    type:TextDecorationType.LineThrough
  })
}

// 统一的卡片样式
@Styles function card(){
  .width('95%')
  .padding(20)
  .backgroundColor(Color.White)
  .borderRadius(15)
  .shadow({radius: 6, color: '#1F000000', offsetX: 2, offsetY: 4})
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较大的任务,我会尽力回答你的问题。首先,你需要安装以下软件: 1. Node.js:用于运行Vue CLI和npm。 2. Visual Studio Code:一个常用的代码编辑器。 3. Vue CLI:用于创建Vue项目的脚手架工具。 接下来,你可以按照以下步骤创建一个Vue项目: 1. 打开终端或命令行工具,在任意目录下创建一个新的项目文件夹,例如"my-project"。 2. 进入该文件夹,在终端中运行以下命令: ``` npm install -g vue-cli vue init webpack my-project ``` 这将使用Vue CLI创建一个新的Vue项目。 3. 接下来,你需要安装Element UI组件库。在终端中运行以下命令: ``` npm install element-ui --save ``` 4. 修改Vue项目中的main.js文件,以便在项目中使用Element UI组件。将以下代码添加到文件的顶部: ``` import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 5. 现在你可以在Vue项目中使用Element UI组件了。例如,在App.vue文件中添加以下代码: ``` <template> <div> <el-button>这是一个Element UI按钮</el-button> </div> </template> ``` 6. 最后,在终端中运行以下命令启动Vue项目: ``` npm run dev ``` 这将启动一个本地开发服务器,并在浏览器中打开你的Vue项目。现在你可以在你的项目中使用Element UI组件了。 希望这些步骤可以帮助你开始使用Vue2 + Element UI构建项目。如果你有其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值