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)
}
网友评论