I created non-recomposing size modifiers to avoid ...
# android
e
I created non-recomposing size modifiers to avoid too many recompositions during animations or user gestures.
Copy code
// regular modifier
Modifier.size(size)

// new modifier
Modifier.size { size } 

// there are also equivalents for width and height
I've shared the code here in this gist which you are free to copy https://gist.github.com/elyesmansour/43160ae34f7acbec19441b5c1c6de3ab ๐Ÿ™‚
๐Ÿ˜ฎ 3
๐Ÿ‘๐Ÿผ 1
๐Ÿ‘ 3
๐Ÿ‘๐Ÿพ 2
๐Ÿ™Œ๐Ÿพ 1
j
Could you explain how this works? I don't understand why this wouldn't trigger a recomposition ๐Ÿ™‚
e
Sure thing ๐Ÿ™‚ The regular size modifiers affect the composition phase, which causes too many recompositions when animating a composable's size through them, and possibly causing performance issues. To avoid this, we'd have to update the size during the layout phase instead using the
layout
modifier, but that code can be cumbersome to write every time. So I decided to just write these handful of modifiers that do the heavy lifting for us and are as easy to use as the regular ones we're used to. The only difference is that they only animate the size during the layout phase without causing performance issues. The reason why they can animate the changes during the layout phase instead of composition phase, is because the state read is deferred thanks to passing a lambda instead of the direct state value.
๐Ÿ‘ 1
j
Thats great. Thanks for the explanation!
o
It looks like the offset modifier works like this
e
Yep, the offset modifier has a lambda parameter to defer the state read to the draw phase.
๐Ÿ‘ 1