文章目录前言适合放在哪些页面里页面结构拆一下核心逻辑说明完整代码最后提醒一句前言这组案例开始进入选择类和调节类组件写业务页面时会经常遇到。这个案例围绕Toggle 设置面板展开重点不是把属性背下来而是弄清楚状态、组件和用户操作之间怎么连起来。这里更适合从数据驱动角度看。数组变化之后ForEach负责把选项渲染出来。适合放在哪些页面里如果把它放到真实项目里常见位置可能是设置页、筛选页、编辑页或者某个需要快速操作的工具面板。用户点一下、选一下、拖一下页面就应该给出明确反馈。维度内容案例主题Toggle 设置面板核心 APIForEach使用场景设置项文章侧重点数据驱动页面结构拆一下代码结构可以按三层读外层负责页面背景和留白中间卡片承载主要内容内部组件处理具体交互。这样的层级不花哨但维护起来比较舒服。我比较推荐先把状态字段命名清楚。选中项、开关状态、滑块数值最好都能从名字看出用途比随手写flag、value后面省心很多。核心逻辑说明先看一段连续代码基本能判断这个案例的运行方式。它包含入口组件、状态字段以及一部分核心 UI。interfaceSettingsTogglePanelItem{label:stringicon:stringstate:stringcolor:string}EntryComponentstruct SettingsTogglePanel{StateisShow:booleantrueStatewifi:booleantrueStatebluetooth:booleanfalseStatelocation:booleantrueStatesilent:booleanfalseStateairplane:booleanfalsebuild(){Column(){这段代码可以拆成几块看State保存当前页面状态用户操作之后会触发 UI 更新。ForEach是案例的关键入口真正的行为通常由它和事件回调一起完成。布局属性服务于业务表达选中、禁用、拖动这些状态最好都能在视觉上看出来。完整代码下面是整理后的完整代码。组件名已经改成SettingsTogglePanel共享颜色也内置到当前文件里单独复制出来读会更方便。constCOLOR_OPTIONS:Array{name:string;value:string}[{name:蓝色,value:#4D96FF},{name:绿色,value:#6BCB77},{name:黄色,value:#FFD93D},{name:红色,value:#FF6B6B},{name:紫色,value:#9B59B6},{name:橙色,value:#FFA500},{name:青色,value:#00BCD4},{name:粉色,value:#FF69B4},{name:灰蓝,value:#607D8B},{name:深色,value:#333333}]constPAGE_BG_COLOR:string#F5F6FAconstCARD_BG_COLOR:string#FFFFFFconstSUBTEXT_COLOR:string#8A8A8AinterfaceSettingsTogglePanelItem{label:stringicon:stringstate:stringcolor:string}EntryComponentstruct SettingsTogglePanel{StateisShow:booleantrueStatewifi:booleantrueStatebluetooth:booleanfalseStatelocation:booleantrueStatesilent:booleanfalseStateairplane:booleanfalsebuild(){Column(){if(this.isShow){Scroll(){Column(){Text(Toggle 多开关 - 设置面板).fontSize(16).fontWeight(FontWeight.Bold).margin({bottom:12})ForEach([{label:Wi-Fi,icon:,state:wifi,color:COLOR_OPTIONS[5].value},{label:蓝牙,icon:,state:bluetooth,color:COLOR_OPTIONS[5].value},{label:定位服务,icon:,state:location,color:COLOR_OPTIONS[3].value},{label:静音模式,icon:Mute,state:silent,color:COLOR_OPTIONS[1].value},{label:飞行模式,icon:✈️,state:airplane,color:COLOR_OPTIONS[0].value}],(item:SettingsTogglePanelItem){Row(){Text(item.icon).fontSize(20)Column(){Text(item.label).fontSize(14).fontWeight(FontWeight.Medium)Text(this.getStatus(item.state)).fontSize(11).fontColor(SUBTEXT_COLOR)}.alignItems(HorizontalAlign.Start).margin({left:10}).layoutWeight(1)Toggle({type:ToggleType.Switch,isOn:this.getState(item.state)}).selectedColor(item.color).onChange((v:boolean){this.setState(item.state,v)})}.width(100%).padding({top:12,bottom:12}).border({width:{bottom:0.5},color:#F5F5F5})})}.width(100%).padding(16).backgroundColor(CARD_BG_COLOR).borderRadius(12)}}Text(多开关设置面板Toggle 多开关 - 设置中心多开关控制).fontSize(12).fontColor(SUBTEXT_COLOR).margin({top:12})}.width(100%).height(100%).backgroundColor(PAGE_BG_COLOR).padding(16)}getState(key:string):boolean{switch(key){casewifi:returnthis.wifi;casebluetooth:returnthis.bluetooth;caselocation:returnthis.location;casesilent:returnthis.silent;caseairplane:returnthis.airplane;default:returnfalse}}setState(key:string,value:boolean){switch(key){casewifi:this.wifivalue;break;casebluetooth:this.bluetoothvalue;break;caselocation:this.locationvalue;break;casesilent:this.silentvalue;break;caseairplane:this.airplanevalue;break}}getStatus(key:string):string{switch(key){casewifi:returnthis.wifi?已连接:未连接;casebluetooth:returnthis.bluetooth?已开启:未开启;caselocation:returnthis.location?已授权:未授权;casesilent:returnthis.silent?已静音:正常;caseairplane:returnthis.airplane?已开启:已关闭;default:return}}}最后提醒一句继续扩展的话我会把静态选项抽成接口返回的数据再补上空状态和异常状态。示例页面先把核心交互跑通就够了到了业务页面状态边界才是真正容易出问题的地方。