鸿蒙ArkUI实现开关switch组件

鸿蒙ArkUI官方提供的toggle组件实现了开关的样式,但在使用过程中还是非常的不方便。

DIY可视化对鸿蒙ArkUI实现开关switch组件扩展后满足基本的switch需求,支持绑定值、设置标题文本、整个背景样式等。

复制代码
/**
 * 开关
 */
@Component
export default  struct DiygwSwitch{
  //绑定的值
  @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 justifyContent:FlexAlign = FlexAlign.End
  @State pointColor: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
        })
      }

      Flex({
        alignItems:this.isWrapLabel?ItemAlign.Start:ItemAlign.Center,
        justifyContent:this.justifyContent
      }){
        Toggle({ type: ToggleType.Switch, isOn: this.value==1 })
          .selectedColor(this.selectedColor)
          .switchPointColor(this.pointColor)
          .height(40)
          .onChange((isOn: boolean) => {
            this.value = this.value==1?0:1
          })
      }
    }.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})
  }
}

import {
    navigateTo
} from '../common/Page'
import {
    IDynamicObject
} from '../component/IType';
import DiygwSwitch from '../component/Switch'
@Entry
@Component
export struct User17069303301033d7f5a1283 {
    @State switched: number = 1;
    @State switch1: number = 1;

    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
                }) {
                    DiygwSwitch({
                            label: '开关',
                            value: $switched
                        })
                        .width('100%')
                    DiygwSwitch({
                            label: '开关',
                            value: $switch1
                        })
                        .width('97.33%')
                        .margin({
                            left: '5vp',
                            right: '5vp',
                            top: '5vp',
                            bottom: '5vp'
                        })
                        .borderRadius({
                            topLeft: '6vp',
                            topRight: '6vp',
                            bottomLeft: '6vp',
                            bottomRight: '6vp'
                        })
                        .backgroundColor("#d6d6d6")
                }.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')
        }
    }


}
相关推荐
TrisighT3 小时前
DevEco Code 写鸿蒙 ArkTS 确实快,但我试了三天后把默认引擎换成了 Cursor
ai编程·harmonyos·cursor
liz7up3 小时前
鸿蒙原生流程图 & 审批流组件 hmflowkit
harmonyos
网易云信20 小时前
全框架覆盖!网易智企IM鸿蒙生态适配再进一步
人工智能·aigc·harmonyos
TrisighT1 天前
我用 AI 逆向了 ArkTS @Builder 的编译产物,看完再也不敢乱写嵌套了
ai编程·harmonyos·arkts
ONEDAY2 天前
HarmonyOS 深色模式适配实践:从资源、WebView 到网络图统一处理
harmonyos
鸿蒙开发3 天前
鸿蒙(HarmonyOS NEXT)表单校验别再手撸正则了 —— 我写了个 ArkTS 版 zod
harmonyos
TrisighT3 天前
ArkTS 的 @BuilderParam 你八成只用了皮毛——那个尾随闭包写法差点被我当 bug 删了
harmonyos·arkts·arkui
ONEDAY4 天前
HarmonyOS 多 Product 构建实践:一套代码生成多个产物
harmonyos
TT_Close4 天前
别劝退了!5秒搞定 Flutter 鸿蒙 FVM 起跑线
flutter·harmonyos·visual studio code
TrisighT4 天前
ArkTS 列表滚动时为什么会闪现旧数据?我扒了 LazyForEach 的复用逻辑
harmonyos·arkts·arkui