美文网首页
flutter 父组件调用子组件方法

flutter 父组件调用子组件方法

作者: 晓函 | 来源:发表于2021-12-28 10:55 被阅读0次

1、在子组件中
重要的是 这句代码 GlobalKey<_ChildState> globalKey = GlobalKey();

import 'package:flutter/material.dart';
 
GlobalKey<_ChildState> globalKey = GlobalKey();
 
class Child extends StatefulWidget {
  Child({
    Key key,
  }) : super(key: key);
  @override
  _ChildState createState() => _ChildState();
}
class _ChildState extends State<Child> {
    
    //子组件方法
    childMethod(){}
    ....
}

2、父组件中

import 'package:flutter/material.dart';
 
class Parent extends StatefulWidget {
  Parent({}) : super(key: key);
  @override
  _ParentState createState() => _ParentState();
}
class _ParentState extends State<Parent> {
    parentMethod(){
        //父组件中调用
        globalKey.currentState.childMethod(_contractOrder)
    }
    @override
    Widget build(BuildContext context) {
        return Child(key: globalKey)
    }

相关文章

网友评论

      本文标题:flutter 父组件调用子组件方法

      本文链接:https://www.haomeiwen.com/subject/mlhpqrtx.html