flutterchina / flutter_in_action_2nd

《Flutter实战 第二版》 书稿
https://book.flutterchina.club/
2.18k stars 343 forks source link

[勘误] 14.6.5 创建新的 PictureLayer #214

Open hanger23 opened 8 months ago

hanger23 commented 8 months ago

原文是: 当绘制完 RepaintBoundary 走到 childContext.stopRecordingIfNeeded() 时, childContext 对应的 Layer 是 OffsetLayer1,而 _currentLayer 是 PictureLayer1, _canvas 对应的是 Canvas1。我们看到实现很简单,先将 Canvas1 的绘制产物保存在 PictureLayer1 中,然后将一些变量都置空。 以下是我的理解:我的是3.3.4版本 当没有 Text5 时,Row的孩子只有2个,所以绘制完 RepaintBoundary,就会走到childContext.stopRecordingIfNeeded(),这没问题的。 但当有 Text5 时,Row的孩子有3个,绘制完 RepaintBoundary并不会走到childContext.stopRecordingIfNeeded(),而是在绘制RepaintBoundary前就stopRecordingIfNeeded()了,所以绘制完 RepaintBoundary,再绘制Text5 依然使用了新的 PictureLayer 和 Canvas,所以结论没问题。只有所有孩子绘制完毕,才会走到childContext.stopRecordingIfNeeded()。

看源码: void paintChild(RenderObject child, Offset offset) { //如果子节点是绘制边界节点 if (child.isRepaintBoundary) { //结束上一个兄弟的绘制,保存绘制产物到父的 pictureLayer,并_canvas 置空 stopRecordingIfNeeded(); if (child._needsPaint || !child._wasRepaintBoundary) { //调用repaintCompositedChild,内部产生新的PaintingContext repaintCompositedChild(child, debugAlsoPaintedParent: true); } else { ... } ... } else { //绘制子项 child._paintWithContext(this, offset); } } 不知道我的理解是否有误?