https://kotlinlang.org logo
#compose
Title
# compose
b

Burhan Kamran

12/05/2023, 7:44 AM
Hello Everyone. Can you guide me about the Modifiers in Compose? What is the purpose and use cases of Modifier.then(), Modifier.merge() & Modifier.composed?
v

Vlad

12/05/2023, 3:46 PM
One of the common usages for me for Modifier.then is to conditionally add something to the initial modifier depends on other fields. For simple example:
Copy code
@Composable
fun MyText(
    modifier: Modifier,
    value: String
) {
    Text(
        modifier = modifier
            .then(
                other = if (value.isEmpty()) {
                    Modifier
                        .background(color = Color.Red)
                } else {
                    Modifier
                }
            ),
        text = value,
    )
}
Pretty much sure those function could very useful if you actually have heavy usage of modifiers and pass the same instance of some base modifier to different functions
z

Zach Klippenstein (he/him) [MOD]

12/05/2023, 7:07 PM
Modifier.composed
is basically deprecated
b

brandonmcansh

12/05/2023, 9:15 PM
@Zach Klippenstein (he/him) [MOD] what’s the replacement?
z

Zach Klippenstein (he/him) [MOD]

12/05/2023, 9:16 PM
Modifier.Node and related APIs
b

brandonmcansh

12/05/2023, 9:17 PM
Ah duh.
3 Views