Lorenzo Benevento
08/23/2021, 6:01 PMLorenzo Benevento
08/23/2021, 7:38 PMColton Idle
08/23/2021, 8:05 PMLorenzo Benevento
08/23/2021, 9:50 PMval buttonWidth by fromButtonToIndicator.animateDp(
transitionSpec = {
tween(durationMillis = 1500)
},
label = "Button Width"
) {
if (it) 240.dp else Dp.Unspecified
}
Lorenzo Benevento
08/23/2021, 9:52 PMLorenzo Benevento
08/23/2021, 9:55 PMIan Ribas
08/24/2021, 2:57 PMDp.Unspecified
is actually Float.NaN
, and so the underlying interpolator doesn’t know what to do with it, unfortunately. So there is no transition, the end value is attributed directly because there is no way to “smoothly” transition from or to NaN
.
I managed an effect similar to what I think is desired by remembering the “self measured and assigned dimension” and using that instead of Dp.Unspecified
for the value to be animated.
It isn’t very pretty and for it to work, the very first time the component is shown, it has to be shown with Dp.Unspecified
.
This is the most important parts of the code I used to test, and it is by no means generic or tested:
var isIndicator by remember { mutableStateOf(false) }
var calculatedWidth by remember { mutableStateOf(0) }
val buttonWidth by animateDpAsState(
targetValue = if (calculatedWidth == 0) Dp.Unspecified else if (isIndicator) 40.dp else LocalDensity.current.run { calculatedWidth.toDp() }
)
...
modifier = Modifier
.width(buttonWidth)
.onSizeChanged {
if (it.width > calculatedWidth) {
calculatedWidth = it.width
}
}