Hi guys! I've a little question about returning a ...
# compose
a
Hi guys! I've a little question about returning a Modifier from interface function. If I have something like this:
Copy code
interface SampleInterface {
     fun modifier(param1, params...): Modifier
}
The function is showing a warning: "Modifier factory functions should be extensions on Modifier". If the function was a normal extension function over Modifier this was not a problem, but it's a function of an interface, so you do know how I can avoid this warning? All implementatios of the interface, starts from the original Modifier and adds some params.
j
In Kotlin, even instance methods can have receivers:
Copy code
interface Sample {
  fun Modifier.something(param1: Type): Modifier
}
a
thanks! And how I could start the modifier params chainning of a composable with this implementation?
j
The same way you do it with any other Modifier? If you are looking for examples,
BoxScope
has Modifier methods as well, for example
Modifier.align
.
a
perfect, thanks! I suppose that there is not any other way to use that sample interface without making a custom scope for a component that needs to use the implementation, no?
j
Well, you could do
with(mySample) { Modifier.something(...) }
👆 1
s
Copy code
with(YourSampleImplementation) {
  Modifier.something()
}
You need to have your Sample interface in the context, same with how BoxScope is in the context when you’re inside the Box composable lambda
And to use it in a Modifier chain use
then
Copy code
Modifier.fillMaxSize().then(with(mySample) { Modifier.something(...) })
a
thanks so much! 😊
e
or put the whole chain inside the scope,
Copy code
with(mySample) {
    Modifier
        .fillMaxSize()
        .something()
💪 1