Flutter Widgets Elements RenderObjects的一点理解

首先放上一张咸鱼技术的图

image.png

第一列是我们开发中常用的
一个StatefulWidget 通过build函数返回一个根ViewRow的Widget,这样写有时候我觉得很奇怪,一个Widget类内部方法build又返回了Widget对象。

根据Widget
Describes the configuration for an [Element]
Widgets are the central class hierarchy in the Flutter framework. A widget is an immutable description of part of a user interface. Widgets can be inflated into elements, which manage the underlying render tree.

描述这个类相当于一个Element的配置类,是Flutter框架的核心类,Widget可以生成Elements,Elements管理渲染树,也就说不是实际的渲染对象。
这个时候就有个问题 Widget都会对应一个Element这个没问题,那一个Element都会对应一个RenderObject吗?

答案是不是的?StatefulWidget和StatelessWidget本身的Element不会有与之对应的RenderObject,他们对应的是ComponentElement .

看下ComponentElement的描述。
Rather than creating a [RenderObject] directly, a [ComponentElement] creates [RenderObject]s indirectly by creating other [Element]s. 这个类是通过其他Widget来创建RenderObject的。
所以咸鱼技术最后一列的图上就没有了StatefulWidget的身影。当时看这个图有点奇怪,所以自己摸索一下,有错误的地方请大佬们指出来。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容