Flutter 父組件調用子組件方法

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,
    );
  }
}
相關文章
相關標籤/搜索