Slackbot
07/08/2024, 12:22 PMNthily
07/08/2024, 12:31 PMRow {
Text("Hello")
Box(
modifier = Modifier.padding(16.dp)
// .thenIf(true) { border(10.dp, Color.Red) }
.border(10.dp, Color.Red)
.size(60.dp)
.background(Color.Blue)
)
Text("World!")
}
result is :Nthily
07/08/2024, 12:32 PMRow {
Text("Hello")
Box(
modifier = Modifier.padding(16.dp)
.thenIf(true) { border(10.dp, Color.Red) }
.size(60.dp)
.background(Color.Blue)
)
Text("World!")
}
result is:Nthily
07/08/2024, 12:33 PMNthily
07/08/2024, 12:37 PMinline fun Modifier.thenIf(
condition: Boolean,
modifier: Modifier.() -> Modifier,
): Modifier = if (condition) this.then(modifier()) else this
Nthily
07/08/2024, 12:39 PMinline fun Modifier.thenIf(
condition: Boolean,
modifier: Modifier.() -> Modifier,
): Modifier = if (condition) modifier() else this
and
Row {
Text("Hello")
Box(
modifier = Modifier.padding(16.dp)
.thenIf(true) { border(10.dp, Color.Red) }
.size(60.dp)
.background(Color.Blue)
)
Text("World!")
}
the result will be the same as the first oneNthily
07/08/2024, 1:08 PMthen
method? 🤔Khubaib Khan
07/08/2024, 1:13 PMthenif
modifier. Basically, it's used as alternative of clickable.Sergey Dmitriev
07/08/2024, 1:52 PMif (condition) modifier() else this
will return modifier()
or receiver’s Modifier
When this.then()
version will return receiver’s Modifier
+ modifier()
or just receiver’s Modifier
Nthily
07/08/2024, 1:54 PMinline
, the if (condition) modifier() else this
block will be replace by Modifier.xxxx.modifier()
🤔Sergey Dmitriev
07/08/2024, 2:12 PMthis.then
is actually en error: https://googlesamples.github.io/android-custom-lint-rules/checks/ModifierFactoryUnreferencedReceiver.md.html
I think the compiler gets confused because there is still an occurrence of this
in the body, it’s there but used wrongly 🤔eygraber
07/08/2024, 2:24 PMthis.then(modifier())
is causing the receiver to be used twiceeygraber
07/08/2024, 2:24 PMthis.then(this.modifier())
which is using the padding modifier twice, hence the larger padding.Nthily
07/08/2024, 2:33 PMZulfadhli
07/08/2024, 3:37 PMthen(Modifier.modifier())
shikasd
07/09/2024, 11:28 AMmodifier()
usually already does this.then
for you
the correct version is if (condition) this.modifier() else this
Nthily
07/09/2024, 11:31 AM