https://kotlinlang.org logo
Title
m

Micko Cabacungan

03/05/2022, 12:14 AM
Does anyone have an idea on why
animateDpAsState
is going way past the
targetValue
(I checked by logging in
onSizeChange
modifier)? I've also specified a
widthIn
max width but that also just gets totally ignored.. Source code example in thread for more context
BoxWithConstraints(modifier = Modifier.widthIn(max = parentMaxSize.width.dp)) {
        //maxWidth is about 360dp from BoxWithConstraints
        val targetDp = 360.dp
        val size by animateDpAsState(
            targetValue = targetDp,
            animationSpec = tween(
                durationMillis = 2000,
                easing = FastOutLinearInEasing
            )
        )
        
        Box(
            modifier = Modifier
                .heightIn(min = 8.dp, max = 8.dp)
                .widthIn(min = 10.dp, max = maxWidth) //we use maxWidth of BoxWithConstraints
                .width(size)
                .background(linearProgressGradientColor, RectangleShape)
                .onSizeChanged {
                    Log.d("@@@", "Bar size: ${it.width.dp}") //somehow this is logging way more than 360dp where it should of stopped resizing itself at targetValue which is 360dp..
                }
        ) {
        }
    }
t

tad

03/05/2022, 2:31 AM
You don't need BoxWithConstraints here, just use
fillMaxWidth()
on the nested Box
d

Doris Liu

03/05/2022, 5:50 AM
Have you tried logging the animation value directly? I suspect what you are seeing is due to
Modifier.width
rather than
animateDpAsState
going beyond target.
Modifier.width
declares a preferred width, which can be overwritten by the incoming constraints from the parent modifier. If you need the width to be exact, consider using
Modifier.requiredWidth
🙂
🙌 1
m

Micko Cabacungan

03/05/2022, 4:20 PM
@tad I needed the box constraint to get the maxWidth availale from the parent
@Doris Liu I did log it, and it was correct, but I'll tried require width! Ty
👍 1