myanmarking
09/28/2021, 2:08 PMmodifier.run {
if(featureA){
modifierX()
} else{
this
}
}
mattinger
09/28/2021, 2:15 PMfun Modifier.conditionalModifier(condition: Boolean, factory: Modifier.() -> Modifier): Modifier =
if (condition) {
factory()
} else {
this
}
Modifier.conditionalModifier(true) { height(4.dp) }
Job Guldemeester
09/28/2021, 2:18 PMModifier.then
as seen in the Surface
composable:
modifier .shadow(elevation, shape, clip = false)
.then(
if (border != null) Modifier.border(border, shape)
else Modifier
)
.background(
color = backgroundColor,
shape = shape
)
myanmarking
09/28/2021, 2:18 PMmattinger
09/28/2021, 2:20 PMmattinger
09/28/2021, 2:21 PMmattinger
09/28/2021, 2:22 PMmyanmarking
09/28/2021, 2:28 PMZach Klippenstein (he/him) [MOD]
09/28/2021, 2:32 PMval border = if() Modifier.border(…) else Modifier
…
modifier
.shadow(…)
.then(border)
.background…
myanmarking
09/28/2021, 2:33 PMmyanmarking
09/28/2021, 2:33 PMCasey Brooks
09/28/2021, 2:55 PMinline fun Modifier.thenIf(
condition: Boolean,
block: ()->Modifier
) = this.then(if(condition) block() else Modifier)
Besides that, using the .then(if...)
syntax seems to be the most common from the Compose sourcesTiago Nunes
09/28/2021, 3:06 PMWaqas Tahir
09/29/2021, 5:14 PMModifier.then(
if(condition){
Modifier.firstModifier()
}else{
Modifier
}
)
I saw this being used inside compose libraries