flutter 如何获得根节点的RenderBox
可以通过GlobalKey来获得根节点的RenderBox,具体步骤如下:
- 在根节点的
Widget上设置一个GlobalKey,例如:
final GlobalKey rootKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Container(
key: rootKey,
child: //...
);
}
- 在需要获取根节点的
RenderBox的地方,使用rootKey.currentContext.findRenderObject()方法获取根节点的RenderBox,例如:
RenderBox rootBox = rootKey.currentContext.findRenderObject() as RenderBox;
这样就可以获取到根节点的RenderBox了。
原文地址: https://www.cveoy.top/t/topic/c8ov 著作权归作者所有。请勿转载和采集!