51 lines
1.2 KiB
Plaintext
51 lines
1.2 KiB
Plaintext
|
|
interface ButtonStyle {
|
||
|
|
width: string | number
|
||
|
|
height: string | number
|
||
|
|
}
|
||
|
|
|
||
|
|
class CusButtonModifier implements AttributeModifier<RowAttribute> {
|
||
|
|
public normalImage?: Resource
|
||
|
|
public activeImage?: Resource
|
||
|
|
|
||
|
|
applyNormalAttribute(instance: RowAttribute): void {
|
||
|
|
if (this.normalImage) {
|
||
|
|
instance.backgroundImage(this.normalImage).backgroundImageSize({ width: "100%", height: "100%" })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
applyPressedAttribute(instance: RowAttribute): void {
|
||
|
|
if (this.activeImage) {
|
||
|
|
instance.backgroundImage(this.activeImage).backgroundImageSize({ width: "100%", height: "100%" })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Component
|
||
|
|
export struct CusButton {
|
||
|
|
public text?: string
|
||
|
|
public normalImage?: Resource
|
||
|
|
public activeImage?: Resource
|
||
|
|
public style?: ButtonStyle
|
||
|
|
private modifier: CusButtonModifier = new CusButtonModifier()
|
||
|
|
|
||
|
|
aboutToAppear(): void {
|
||
|
|
this.setAttr()
|
||
|
|
}
|
||
|
|
|
||
|
|
setAttr() {
|
||
|
|
this.modifier.normalImage = this.normalImage
|
||
|
|
this.modifier.activeImage = this.activeImage
|
||
|
|
}
|
||
|
|
|
||
|
|
build() {
|
||
|
|
Row() {
|
||
|
|
if (this.text) {
|
||
|
|
Text(this.text).fontFamily("Alimama").fontSize(24)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
.width(this.style?.width)
|
||
|
|
.height(this.style?.height)
|
||
|
|
.justifyContent(FlexAlign.Center)
|
||
|
|
.attributeModifier(this.modifier)
|
||
|
|
}
|
||
|
|
}
|