https://kotlinlang.org logo
Title
m

MBegemot

06/27/2021, 5:48 PM
I have a question about swipe I'll put the details in a thread
As you see I display a text in a dialog with a tool bar below , there are two arrows pointing right and left that allows me to go the next or previous sentence. In order to minimize the amount of controls I would like to swipe the text right or left to produce the same effect than when I click to the arrows. I'm not that much interested in fancy animations, just a kind of onSwipeLeft and onSwipeRight events. If any can point me in the right direction I will be very thankfully.
@Composable
fun dragablesquare(pTP: PlayTextParams, children: @Composable () -> Unit){
    var offsetX by remember{ mutableStateOf(0f)}
    Box(Modifier
        .offset { IntOffset(offsetX.roundToInt(), 0) }
        .draggable(
            enabled = true,
            orientation = Orientation.Horizontal,
            state = rememberDraggableState { delta ->
                offsetX += delta
            },
            onDragStopped = {
                               if(offsetX>0){          if(pTP.canIscrollOne(PlayTextParams.scroll.FORWARD)) pTP.setIndex(pTP.aIndex+1) }
                               else {
                                   if(pTP.canIscrollOne(PlayTextParams.scroll.BACK))
                                       pTP.setIndex(pTP.aIndex-1)
                               }
                               offsetX=0f
                            },
            onDragStarted = { }
        )
    ){
        children()
    }
}
Sorry for the bad formatting, but it's so crazy simple
So I surround my previous component showing the sentences with dragablesquare{mytextcomponent()} and that's it. It's not a general component but a really fast working solution.