I have a Stack widget which hosts a Box and an Image. As the state changes, I want to scale the Box widget by whatever value the state has, for example by 2x.
I couldn't find anything about scaling the widgets on the Modifier or Box properties so I decided to react to the state changes by manipulating the size using "Modifier.size" which is not ideal for me.
So is there support for scaling the widgets or should I manually play with the size property?
-Thanks
@Composable
fun Pointer(modifier: Modifier = Modifier, state: TransitionState, onClick: () -> Unit) {
Stack(modifier) {
Box(
shape = CircleShape, backgroundColor = Color.Gray.copy(alpha = .3f),
modifier = Modifier.size(state[width])
)
Image(
asset = imageResource(id = R.drawable.ic_pointer),
modifier = Modifier
.clickable(onClick = onClick)
)
}
}
That is going to be a problem. In Compose, widgets like
Box()
are stateless functions. You cannot ask aBox()
how big it is — instead, you need to tell theBox()
how big it is, using a suitableModifier
.Frequently, "how big it is" is a fixed value or rule, set in the code (e.g.,
Modifier.size(200.dp)
). You should be able to have the size be dependent upon some state that you track yourself, so long as that state is aState
, so Compose knows to recompose (call your function again) when thatState
changes. If you go that route, then scaling is a matter of checking the currentState
value, applying your scale factor, and using the result for the newState
value.