子组件调用父组件就用官方的各种callBack就行 

而父组件调用子组件除了eventBus  的通知 (eventBus 应用场景适合组件太多 父子信息交互过于繁琐的时候 )  还可以 用GlobalKey() 

//父组件

...
 InkWell(
            onTap:(){
              childKey.currentState.testFun("233");
            },
            child: Test(key:childKey,)
        ),

...

//子组件


import 'package:flutter/cupertino.dart';

GlobalKey<_State> childKey = GlobalKey();
class Test  extends StatefulWidget {
  Test({
    Key key,
  }) : super(key: key);
  @override
  _State createState() => _State();
}

class _State extends State<Test> {

  testFun(String test){
    print("____触发子组件");
    print("${test}");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child:Text("子组件"),
    );
  }
}
 
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐