zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Flutter 父组件调用子组件方法

2023-09-27 14:28:53 时间
class father extends StatelessWidget {
  var child;

  @override
  Widget build(BuildContext context) {
    child = son();

    return Scaffold(
      body: child,
      floatingActionButton: FloatingActionButton(
        onPressed: () => child?.childFunction(),
      ),
    );
  }
}

class son extends StatelessWidget {
  void childFunction() => print('called in child widget');

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
    );
  }
}