Flutter中的三棵树是如何完成绘制的
定义 Widget:存放渲染内容、它只是一个配置数据结构,创建是非常轻量的,在页面刷新的过程中随时会重建。 其中Widget根据功能可以分为三类: 组合类StatelessWidget/StatefulWidget:比如我们常见的Container就是一个组合类的控件,它主要负责组合封装多个其他负责绘制的原子组件。 代理类inheritedwidget:它的父类是ProxyWidget,顾名思义。简单来说,InheritedWidget 的作用是向它的子 Widget 有效地传播和分享数据,当 InheritedWidget 作为一个Parent Widget时,它下面的Widget tree的所有Widget都可以去和 InheritedWidget 发生数据传递和交互。当数据发生改变时,一部分控件需要 rebuild,另外的控件不需要 rebuild 的时候,可以使用 InheritedWidget。 绘制类RenderObjectWidget:RenderObjectWidget会负责创建出负责实际渲染的RenderObject对象,RenderObject负责实际的layout()和paint()。后期有空,我会手动写一个RenderObject。 Element: 是分离 WidgetTree 和真正的渲染对象的中间层,可以看成flutter的骨架。 Widget 用来描述对应的Element 属性,同时持有Widget和RenderObject,存放上下文信息,通过它来遍历视图树,支撑UI结构。 其中Element可以根据功能分成两大类: 组合类ComponentElement:主要包括如下 StatelessElement / StatefulElement / ProxyElement 子类;其中各Element都是与 Widget 对应的。 绘制类RenderObjectElement:RenderObjectElement对应的是RenderObjectWidget。 RenderObject用于应用界面的布局和绘制,负责真正的渲染,保存了元素的大小,布局等信息。 当应用启动时 Flutter 会遍历并创建所有的 Widget 形成 Widget树,通过调用 Widget 上的 createElement() 方法创建每个 Element 对象,形成 Element 树。最后调用 Element 的 createRenderObject() 方法创建每个渲染对象,形成一个 RenderObject 树。 Widget => Element 如何通过Widget更新Element: Element/ComponentElement: 单个Element时候通过updateChild方法更新; E } l e i } f i } } r m / f i f e e 如 i r n i } } } e n t n 果 ( f e a ( / f l e u t n n d t l c 新 i n e i c n e d n s o w r ? e e ( e u h 旧 ( f e l / f h e l / e e e l C n w w c a r E i w c u w s 可 u i w s 其 a w d h u W W h c n l l i h ( p C e 以 ( p l C e 他 c C { i n p i i i t e d d i c d h u c d d h 情 t h c l e d d d l i n m g l h a i i p h a . i { 况 i i h d w a g g d v u e ! e d i t l f d i t u l , v l i C t e e a l n = t . l e d a l e p d 移 a d l = h e t t ! t l t 相 w d S ( t d S d 除 t d i C 是 = e ; n 同 i . l = W e . l a = 旧 e = 是 i l h n = C n u 的 d s o i 的 s o t 的 C n n d i u = n h e l 情 g l t c d 情 l t e c , h i u f ; l l u i w l 况 e o F h g 况 o F ( h 重 i n l l d l n l l C ) t t o i e , t o n i 新 l f l a ( , u l d h r l t 也 r e l i d l , t E 并 l ) ( i { = ! C d . 就 ! C w d n ( a 这 e l 且 l c l = = h ; c 是 = h W ; f c t 里 W e o ) h d i a r i i l h e 直 i m l i ; n n l n u n l d a i W 接 d e d { l e e d U n e d g t l i i g n d w w ( p t w ( e e d d n e t c ) W S c d i S c t W ) g f t ? h ; i l h a m l h ) i ; e l ( i d o i t e o i ; d t a n c l g t l e t t l g ( t e h d e ) d ( y ) d e n e w i 非 t , c p , t e 新 W l n ) h e 新 w 的 i d u n i 和 n 的 W w d , l { e l k e w i i g l w d e w i d d e W , S . y S d g g t i 直 l w 相 l g e e , d 接 o i 同 o e t t g d t d t t , , n e e ) g ) , 会 e t a ; e ; 会 n 创 w ? c t 创 e 建 S t , 建 w e l n i e S l o e v n l l e t w a e e o m ) W t w m t e ; i e W e ) n d C i n ; t g h d t e i g t l e , d t ) O ) b j { e c t ? n e w S l o t ) { 大致归纳一下,如下表: ...