How to Use Conditional Modifiers In Jetpack Compose

preview_player
Показать описание
Follow for more Android & Kotlin tips 🙌
Рекомендации по теме
Комментарии
Автор

Nice example.
How is this different from this:
Column(modifier = if(isPhone) {

} else {
Modifier
}) {

}

segunfrancis
Автор

I refactor my code after every Short video that you drop! 🔥🔥🔥🔥

ntikomathaba
Автор

You can make a generic extension function of the Modifier class that takes both a condition and a Modifier operation, and that applies it if condition is true otherwise returns "this"

Drackmord
Автор

Is it not the same as this?
Modifier.let {
if (isPhone) {

} else {
it
}
}

juanherrera
Автор

it works but I think an extension would be cleaner. Even better solution, in this case, is passing it in the parameter: verticalScroll(enabled = condition)

soboleum
Автор

And then put this kind of things in a utility class to use them elsewhere

yassinesafraoui
Автор

Pretty cool. But wouldn't a val with the condition in be neater?

antpr
Автор

Great #shorts Philipp.

Though, I am having trouble making a dynamic height for Column, Row or Box.
My use case is to make it have a maximum height of my choice only when the content grows upto that, otherwise, the height should not exceed the content hight.
For example, if I want a hight of 200.dp, but the content height is currently at 70.dp, the container height should not exceed that, until it grows to the maximum height defined for the container.

I have tried modifier.height, modifier. requiredHeight and fillMaxHeight but not getting my desired result.

How can I achieve this?

Aweklin
Автор

Would this extension function be a problem? from the looks of it it's slightly better because you skip doing then(Modifier) is the condition false. Also it reads better as applyIf(condition) { padding(16.dp) }

inline fun Modifier.applyIf(take: Boolean, other: Modifier.() -> Modifier): Modifier =
if (take) this.other() else this

Jon
Автор

holy prophet of @Compouse, thank you for another revelation!

alexnovikov
Автор

Is this different from Modifier.let ? I mean, write the condition with the let argument "it" and return "it" or the other "it.verticalScroll() "?

frukoyurdakul
Автор

fun Boolean, modifier: Modifier.() -> Modifier): Modifier {
return if (condition) {
then(modifier(Modifier))
} else {
this
}
}

mohegyux
Автор

also you can use this beautiful code :)

inline fun Modifier.ifFalse(value: Boolean, crossinline block: Modifier.() -> Modifier): Modifier =
if (!value) block.invoke(this) else this

inline fun Modifier.ifTrue(value: Boolean, block: Modifier.() -> Modifier): Modifier =
if (value) block.invoke(this) else this

osfhfhh