PageA的定义如下
class PageA extends StatefulWidget {
PageA({Key key}) : super(key: key);
@override
PageAState createState() {
return PageAState();
}
}
class PageAState extends State<PageA> {
static GlobalKey homeKey = GlobalKey();
//关键代码
static currentInstance() {
var state = PageAState.homeKey.currentContext.findAncestorStateOfType();
return state;
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return return Scaffold(
key: homeKey,
body: Container();
);
}
reloadData() {
setState(() {});
}
}
在 PageB 里调用 PageAState 的 reloadData() 方法
class PageB {
buttonClick() {
PageAState.currentInstance().reloadData();
}
}