https://kotlinlang.org logo
k

Kazemihabib1996

05/07/2020, 7:50 PM
@romainguy Hi, again, sorry for asking a lot but I have another question about this stuff that's also been asked a lot with no answers in flutter github issues.(e.g https://github.com/flutter/flutter/issues/11002 ) You told that I don't need saveLayer at all in this example. can you please explain when we need to use saveLayer? I tried multiple situations and I got confused: 1)
Copy code
drawRect(rect, paint.apply { color = Color.Red })

saveLayer(Rect.fromLTWH(0f, 0f, size.width.value, size.height.value), Paint().apply { blendMode = BlendMode.dstOut })

drawRect( rect.shift(Offset(20f, 20f)), paint.apply { color = Color.Blue } )

restore()
This example with `BlendMode.dstOut`not works correctly just the blue rectangle becomes black I don't understand why this happens. 2)
Copy code
drawRect(rect, paint.apply { color = Color.Red })

drawRect( rect.shift(Offset(20f, 20f)), paint.apply {
          color = Color.Blue, blendMode = BlendMode.dstOut }
 )
This one is without saveLayer and just by applying the blendMode to the paint it works for
multiply
but doesn't work for
dstOut
and produced the same result like the previous one. 3) But if I call`saveLayer` with default blendMode of Paint, before both drawings in the above example it works I don't even need to
restore
it.
Copy code
saveLayer(Rect.fromLTWH(0f, 0f, size.width.value, size.height.value), paint = Paint())
drawRect(rect, paint.apply { color = Color.Red })
drawRect(
                rect.shift(Offset(20f, 20f)),
                paint.apply {
                    color = Color.Blue
                    blendMode = BlendMode.dstOut
                }
        )
I don't understand it, why this happens? based on the docs
Copy code
* Saves a copy of the current transform and clip on the save stack, and then
     * creates a new group which subsequent calls will become a part of. When the
     * save stack is later popped, the group will be flattened into a layer and
     * have the given `paint`'s [Paint.colorFilter] and [Paint.blendMode]
     * applied.
I think the
saveLayer
must be called between the first and second
drawRect
and then I should call the
restore
but here I should call it before both drawings and even I don't need to call
restore
.