鸿蒙ArkUI实现的Slider组件

鸿蒙ArkUI实现的Slider组件进行二级封装后适用于表单组件里适用,显示当前slider值,最小值,最大值设计等。

/**
 * 滑块
 */
@Component
export default  struct DiygwSlider{
  //绑定的值
  @Link value:number;
  //未选中图标
  @State labelImg: Resource = $r('app.media.user');
  //是否文本图片
  @State isLabelImg: boolean = false;
  @State labelImgWidth: number = 20;
  @State labelImgHeight: number = 20;
  //标题文本
  @State label:string = '滑块';
  //水平状态时,文本占大小
  @State labelWidth:number = 80;
  //是否标题文本换行
  @State isWrapLabel:boolean = false;
  //是否标题文本
  @State isLabel:boolean = true;
  //标题颜色
  @State labelColor:string = "#333333";
  //自动标题长度
  @State isLabelAuto:boolean = false;
  //文本字体大小
  @State textSize:number = 14;
  //选中图版本大小
  @State imgSize:number = 28;
  //组件内边距
  @State leftRightPadding:number = 16;
  @State topBottomPadding:number = 6;

  @State min: number = 0
  @State max: number = 100
  @State step: number = 1
  @State blockColor:string = "#fff";
  @State selectedColor:string = "#07c160";
  @State isShowValue:boolean = true;
  @State isBorder:boolean = true;
  build() {
    Flex({
      alignItems:this.isWrapLabel?ItemAlign.Start:ItemAlign.Center,
      direction:this.isWrapLabel?FlexDirection.Column:FlexDirection.Row,
      justifyContent:FlexAlign.Start
    }){
      if(this.isLabel){
        Row(){
          if(this.isLabelImg){
            Image(this.labelImg)
              .width(this.labelImgWidth)
              .height(this.labelImgHeight)
              .margin({ left:3 }).flexShrink(0)
          }
          if(this.isLabelAuto){
            Text(this.label).flexShrink(0).fontColor(this.labelColor).fontSize(this.textSize).margin({
              bottom:this.isWrapLabel?10:0,
              right:10,
            }).textAlign(TextAlign.Start);
          }else{
            Text(this.label).fontColor(this.labelColor).width(this.isWrapLabel?'100%':this.labelWidth).fontSize(this.textSize).margin({
              bottom:this.isWrapLabel?10:0
            }).textAlign(TextAlign.Start);
          }
        }.margin({
          top:this.isWrapLabel?10:0
        })
      }

      Slider({ value: this.value, min: this.min, max: this.max, step: this.step })
        .blockColor(this.blockColor)
        .selectedColor(this.selectedColor)
        .onChange((value: number, _) => {
          this.value = value
        })
        .width('100%')

      if(this.isShowValue){
        Text(`${Math.floor(this.value)}`)
          .textAlign(TextAlign.End)
          .width(this.textSize*3)
          .fontColor(this.labelColor)
          .fontSize(this.textSize)
      }
    }.borderWidth({
      bottom: this.isBorder?1:0
    }).borderColor({
      bottom: "#eee"
    }).borderStyle(BorderStyle.Solid).borderStyle(BorderStyle.Solid).height(this.textSize+(this.isWrapLabel?20:0)+30+this.topBottomPadding*2).padding({left:this.leftRightPadding,right:this.leftRightPadding,top:this.topBottomPadding,bottom:this.topBottomPadding})
    // .onAppear(() => {
    //   this.initCheck()
    // })
  }
}

使用也是相当的简单

import {
    navigateTo
} from '../common/Page'
import {
    IDynamicObject
} from '../component/IType';
import DiygwSlider from '../component/Slider'
@Entry
@Component
export struct User {
    @State slider: number = 66;

    async onPageShow() {}

    async aboutToAppear() {
        await this.onPageShow()
    }

    build() {
        Column() {
            Navigation()
                .width('100%')
                .height('56vp')
                .backgroundColor('#07c160')
                .title(this.NavigationTitle())
                .titleMode(NavigationTitleMode.Mini)
                .align(Alignment.Center)
                .hideBackButton(true)
            Scroll() {
                Flex({
                    direction: FlexDirection.Column
                }) {
                    DiygwSlider({
                        label: '滑块',
                        value: $slider
                    })

                }.height('100%')
            }.height('100%').layoutWeight(1)
        }.alignItems(HorizontalAlign.Start).height('100%')
    }

    @Builder
    NavigationTitle() {
        Column() {
            Text('个人中心')
                .width('100%')
                .textAlign(TextAlign.Center)
                .height('28vp')
                .fontSize('20fp')
                .fontWeight(500)
                .fontColor('#fff')
        }
    }


}

 

你可能感兴趣的:(ArkUI,ArkTs,鸿蒙,harmonyos,低代码)