Hello, is there any way to extract some styles for...
# compose
v
Hello, is there any way to extract some styles for elements? For example I wanted to make something like this:
Copy code
object MyTheme {
        val myButtonColors = ButtonDefaults.buttonColors(
        backgroundColor = Color.Black,
        contentColor = Color.White,
        disabledBackgroundColor = Color.Gray,
        disabledContentColor = Color.DarkGray
    )
}
And then use it on button like this:
Copy code
Button(
            modifier = Modifier.width(140.dp).height(50.dp).pointerHoverIcon(PointerIcon.Hand),
            colors = MyTheme.myButtonColors,
            onClick = { onLoginClicked(username.value, password.value) }) {
            Text("Login")
        }
s
What exactly are you trying to do? Typically, you'd create a theme and wrap your content in it. See: https://developer.android.com/jetpack/compose/designsystems/material3
v
I tried using theme but I cannot understand how to do it. All I want is to have multiple versions of buttonColors, but I do not want to define them directly inside of a button because it looks messy then.
a
what you are trying to do is pretty beneficial for the performance of the compose as it will make the modifiers or styles you are defining called only once when it is accessed and then uses the same instance for the modifier or the style. anyway, you need answers rather than opinions so here you go 😄: • create a specified style holder object. rather than using it inside of MyTheme, create something such as MyListItemModifiers or MyButtonStyles object. then try to define each variable with a get field and a private set field, and make them lazy initialized as much as possible. • after that, you can inject your styles or modifiers by using a CompositionLocalProvider. you can check them out in the google for more information. • then access your styles with something such as LocalStyleguide.local • ta da! you have a injectable lazy loaded performance monster in compose.