https://kotlinlang.org logo
#compose-android
Title
# compose-android
b

bryankeltonadams

10/06/2023, 10:29 PM
how come I can't use 100.dp.toPx()? I'm following this swipeable to AnchoredDraggable migration guide where they have.
Copy code
enum class DragValue { Start, Center, End }

@Composable
fun AnchoredDraggableBox() {
    val anchors = DraggableAnchors {
        Start at -100.dp.toPx()
        Center at 0f
        End at 100.dp.toPx()
    }
    val state = remember {
        AnchoredDraggableState(anchors = anchors)
    }
    Box(
        Modifier.offset { IntOffset(x = state.requireOffset(), y = 0) }
    )
}
but toPx() isn't an extension function for me.
i found this in the package androidx.compose.ui.unit
Copy code
* Drawing and Layout are done in pixels. To retrieve the pixel size of a Dp, use [Density.toPx]:
but I don't see any functions declared in this file for toPx
m

Mahmoo

10/06/2023, 10:44 PM
Maybe DraggableAnchor dont give you composable scope
It is an extension function declared inside class Density so you need an instance of Density to be able to see this function.
a

ascii

10/07/2023, 12:07 AM
Do something like
LocalDensity.current.run { 100.dp.toPx() }
e

ephemient

10/07/2023, 12:38 AM
it is clearer to
Copy code
with(LocalDensity.current) { 100.dp.toPx() }
than
.run {}
IMO
💯 1
but regardless, contexts where you are dealing with pixel values extend
Density
, e.g.
DrawScope
,
MeasureScope
,
PointerInputScope
, etc.
7 Views