https://kotlinlang.org logo
#compose
Title
# compose
h

Hamba

11/22/2023, 6:10 AM
In this snippet, why does the print maxWidth in the detectTapGestures get closured and doesnt update when the box recomposes upon resizing?
Copy code
BoxWithConstraints {
        println("Box recomposed width: $maxWidth")
        Column(
            modifier
                .pointerInput(Unit) {
                    detectTapGestures { println("Current box width: $maxWidth") }
                }
        ) { }
    }
e.g. if i resize it and then click on the box, it still shows the old width even though it has recomposed.
Copy code
Box recomposed width: 568.8.dp      <- original width
Box recomposed width: 1515.2.dp     <- resized width
Current box width: 568.8.dp         <- printing width on tap still prints old width even after resizing occurred
I'd have thought the column and its modifier would be recomposed from scratch with the updated maxWidth when the Box is recomposed. how can i make it so the maxWidth printed on tap gesture is updated upon the box being recomposed?
b

Ben Trengrove [G]

11/22/2023, 6:36 AM
pointerInput has a size property btw, so you might be able to avoid this altogether?
☝️ 1
👍 1
a

Albert Chang

11/22/2023, 6:37 AM
Because the block of
Modifier.pointerInput
is long-running and won't restart on recomposition automatically, so it only captures the initial value. Read its document for more details.
h

Hamba

11/22/2023, 6:50 AM
thanks!
d

Divyansh Kushwaha

11/22/2023, 9:57 AM
Anyways, if you want to force a restart you can always pass the dependent param as key.