?
I am working on a feature which users can change the
fontScale
within the app. I thought doing sth like this would be elegant:
Copy code
val density = LocalDensity.current.copy(fonScale = customizedFontScale)
CompositionLocalProvider(LocalDensity provides density) {
content()
}
However,
Density
is an interface I can not copy it. Also I found the real implementation is
DensityWithConverter
which is a private class I can not access it as well.
So is there anything I can do or any better approach?
z
Zach Klippenstein (he/him) [MOD]
10/08/2024, 4:49 PM
You’re very close realizing it’s an interface:
Copy code
val parentDensity = LocalDensity.current
val myDensity = remember(parentDensity) {
object : Density by parentDensity {
// override whatever you want
}
}
// provide myDensity
💯 1
l
lesincs
10/08/2024, 9:18 PM
👍 Thanks Zach. It works like a charm. I haven't thought of interface delegation.