HarmonyOS 状态管理问题
怎么在外部的onclick事件中修改JhkDeviceItem中的switch状态。
业务场景:组件内的switch点击后,外部业务处理失败后,需要将switch状态重置回去。
具体代码如下:
import { JuColor } from '../../color/colors'
import { ResManager } from '../../resources_manager/ResManager'
export enum RightType {
// 胶囊按钮
capsuleButton,
// 箭头按钮
arrowButton,
// 开关按钮
switchButton,
}
interface ImageInfo {
url: string;
errorImageUrl?: String|null;
placeholderImageUrl?: String|null;
}
export interface ItemInfo {
image?:ImageInfo|null;
title: string;
description: string;
rightWidgetType: RightType;
// capsuleButton按钮文字
buttonText?: string;
// switch组件状态
isOn?: boolean|null;
// 右侧组件点击回调
onClick?: ((type: RightType, data?: ItemInfo|null) => void)|null
}
@Component
export struct JhkDeviceItem {
@State
itemInfo: ItemInfo|undefined = {
title: '',
description: '',
rightWidgetType: RightType.arrowButton
};
aboutToAppear(): void {
this.itemInfo ??= {
title: '',
description: '',
rightWidgetType: RightType.arrowButton
}
}
@Builder
image() {
if (this.itemInfo?.image) {
Image(this.itemInfo?.image?.url)
.objectFit(ImageFit.Contain)
.width('44lpx')
.height('44lpx')
Blank('11lpx')
}
}
@Builder
rightWidget() {
if (this.itemInfo?.rightWidgetType === RightType.capsuleButton) {
Button(this.itemInfo?.buttonText, {
type: ButtonType.Normal,
stateEffect: true,
})
.backgroundColor(JuColor.ff13bdc0)
.fontSize('12lpx')
.fontWeight(FontWeight.Regular)
.fontColor(Color.White)
.width('54lpx')
.height('23lpx')
.borderRadius('23lpx')
} else if (this.itemInfo?.rightWidgetType === RightType.arrowButton) {
Image(ResManager.getMediaPic('arrow_right'))
.objectFit(ImageFit.Contain)
.width('10lpx')
.height('16lpx')
} else if (this.itemInfo?.rightWidgetType === RightType.switchButton) {
Toggle({
type: ToggleType.Switch,
isOn: this.itemInfo?.isOn ?? false,
})
.onChange((isOn: boolean) => {
this.itemInfo!.isOn = isOn;
this.itemInfo?.onClick?.(this.itemInfo?.rightWidgetType, this.itemInfo);
})
.selectedColor(JuColor.ff13bdc0)
.width('37.5lpx')
.height('19lpx')
}
}
@Builder
content() {
Column(){
Row(){
Text(this.itemInfo?.title)
.fontColor(JuColor.ff1a1a1a)
.fontSize('15lpx')
.fontWeight(FontWeight.Regular)
.textAlign(TextAlign.Start)
.maxLines(1)
.textOverflow({
overflow: TextOverflow.Ellipsis
})
.constraintSize({
maxWidth: '216lpx'
})
Blank()
this.rightWidget()
}
.width('100%')
.alignItems(VerticalAlign.Center)
Blank('8lpx')
Text(this.itemInfo?.description)
.fontColor(JuColor.ff666666)
.fontSize('12lpx')
.fontWeight(FontWeight.Regular)
.textAlign(TextAlign.Start)
.maxLines(2)
.textOverflow({
overflow: TextOverflow.Ellipsis
})
.constraintSize({
maxWidth: '216lpx'
})
}
.layoutWeight(1)
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Start)
}
build() {
Stack(){
Row(){
this.image()
this.content()
}
.justifyContent(FlexAlign.Start)
.alignItems(VerticalAlign.Center)
.padding({
left: '15lpx',
right: '15lpx',
top: '16lpx',
bottom: '16lpx',
})
.width('100%')
// TODO: 将透明按钮放到指定位置
}
.width('347lpx')
.borderRadius('12lpx')
}
}
调用地方:
JhkDeviceItem({
itemInfo: {
image: {
url: 'xxx',
},
title: '测试测试哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈',
description: '哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈',
rightWidgetType: RightType.switchButton,
onClick: (type, data) => {
console.log(`onClick ${data?.isOn}`)
// 出现一些异常时候,重置isOn属性
setTimeout(() => {
data!.isOn = false;
},
2*1000
)
},
}
})
.backgroundColor(JuColor.c8013bdc0)
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
HarmonyOS 状态管理-父子组件同步的问题
865浏览 • 1回复 待解决
状态管理相关问题,有人知道吗?
916浏览 • 1回复 待解决
HarmonyOS 关于状态管理
495浏览 • 1回复 待解决
HarmonyOS 状态管理咨询
621浏览 • 1回复 待解决
HarmonyOS 新状态管理框架@observed、@track
971浏览 • 1回复 待解决
HarmonyOS 状态管理之ObjectLink的疑问
431浏览 • 1回复 待解决
HarmonyOS 界面管理问题
314浏览 • 1回复 待解决
HarmonyOS 嵌套层级较多,导致状态管理不能刷新UI
874浏览 • 1回复 待解决
HarmonyOS 状态管理Provide和Consume无法和NodeContainer使用
423浏览 • 1回复 待解决
HarmonyOS 状态同步问题
442浏览 • 1回复 待解决
HarmonyOS 路由页面管理问题
499浏览 • 1回复 待解决
HarmonyOS 父子组件状态问题
254浏览 • 1回复 待解决
HarmonyOS 变量状态同步问题
618浏览 • 1回复 待解决
#鸿蒙通关秘籍# 如何在HarmonyOS NEXT中进行状态管理?
526浏览 • 1回复 待解决
HarmonyOS @ObservedV2和之前的状态管理可以共用吗
413浏览 • 1回复 待解决
HarmonyOS ComponentV1状态管理和V2无法混用
530浏览 • 1回复 待解决
#鸿蒙通关秘籍#状态管理在ArkTS里怎么玩?
475浏览 • 1回复 待解决
HarmonyOS 嵌套Class状态观察问题
231浏览 • 1回复 待解决
HarmonyOS 相机会话管理相关问题
159浏览 • 1回复 待解决
#鸿蒙通关秘籍#如何管理和渲染HarmonyOS Next组件中的状态?
510浏览 • 1回复 待解决
HarmonyOS 状态管理V2如何使用AppStorage中的相关属性
311浏览 • 1回复 待解决
#鸿蒙通关秘籍#如何在ArkTS实现全局UI状态管理?
552浏览 • 1回复 待解决
状态管理装饰器:state和observerd/objectlink,性能对比
2254浏览 • 1回复 待解决
HarmonyOS 关于@Watch监听状态问题
795浏览 • 1回复 待解决
HarmonyOS 管理Cookie及数据存储路径问题
446浏览 • 1回复 待解决
可以使用@ObservedV2装饰器和@Trace装饰器来控制toggle开关,参考文档为:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-new-observedv2-and-trace-V5
以下是参考demo: