06/10/2024

Fabrikant Tech

Tech Specialists

Custom Modifiers jetpack compose | ProAndroidDev

Custom Modifiers jetpack compose | ProAndroidDev
Custom Modifiers jetpack compose | ProAndroidDev

Take note: this report builds on top of this report so I will remarkably persuade you to check out that article first to recognize how items are doing the job.

As reviewed in the previous article to produce a tailor made modifier we want to give an implementation of Modifier.Component interface. There is a DrawModifier interface which extends Modifier.Aspect so we can also use implmentation of DrawModifier.

Let’s say we want to see our Composable in a body like in graphic down below

There is no current modifier which can do this for you.

so what do we do if something does not exist ? (provide it into existence)

So now allows produce our custom made modifier we simply call it addDecoration() but lets initially go item oriented and crave for a extensible style and design.

I am generating a decoration interface which all long term Decorations will be able to use.

Earlier mentioned body also seems to be of decoration let us go ahead and generate an implementation of Decoration we simply call it Frames which will be a sealed class which will make us able to follow open closed theory without a course explosion and will enable in encapsulating frame like decorations.

The DrawModifier interface has one particular summary approach entertaining ContentDrawScope.draw() that we need to implement for drawing. As we can see attract() is an extension purpose on ContentDrawScope which is popular sample viewed in compose resource code as a substitute of passing the object as system argument we deliver the system as extension perform which will save us from contacting members using . considering the fact that we can immediately accessibility those people.

A ContentDrawScope delivers us sizing of composable at which the modifier is utilized and various drawing strategies like — drawLine() , drawRect(), drawPath() and so on.

now use it in your implementation DrawModifier as follows

We are also calling drawContent() in previously mentioned implementation which is employed for initial drawing of the composable it is analogous to tremendous.onDraw() of a custom view implementation and equivalent to onDraw() order is critical draw your body soon after the initial information if not the frame will be concealed at the rear of since articles is drawn employing painter’s algorithm.

Now this is how you attract a the body, set it in earlier mentioned code.

Use

Outcome