类似私有属性变量的定义,只是属性变量类型是一个方法,通过()=>void进行声明
@Entry
@Component
struct Parent {
parentFunc(){
AlertDialog.show({message:'this is from parent',alignment:DialogAlignment.Bottom,offset:{dx:0,dy:-30}})
}
build() {
Column() {
Child({func:()=>{
this.parentFunc()
}})
}
.height('100%').width('100%')
}
}
@Component
struct Child{
func:()=>void
build(){
Row(){
Button('点我试试').onClick(()=>{
this.func()
}).width('30%').height(40)
}.width('100%').height('100%').justifyContent(FlexAlign.Center)
}
}
效果如下:

本文介绍了如何在Flutter中使用类似私有属性的方法,通过`struct`定义并调用。`structParent`和`structChild`组件展示了如何在`func`中声明并通过点击事件调用`parentFunc`,实现自定义功能的交互式设计。

2460

被折叠的 条评论
为什么被折叠?



