InteractionState

@Stable class InteractionState : State<Set<Interaction>>
kotlin.Any
   ↳ androidx.ui.foundation.InteractionState

InteractionState represents a Set of Interactions present on a given component. This allows you to build higher level components comprised of lower level interactions such as clickable and androidx.ui.foundation.gestures.draggable, and react to Interaction changes driven by these components in one place. For Interactions with an associated position, such as Interaction.Pressed, you can retrieve this position by using interactionPositionFor.

Creating an InteractionState and passing it to these lower level interactions will cause a recomposition when there are changes to the state of Interaction, such as when a clickable becomes Interaction.Pressed.

import androidx.compose.remember
import androidx.ui.foundation.Border
import androidx.ui.foundation.Box
import androidx.ui.foundation.InteractionState
import androidx.ui.foundation.Text
import androidx.ui.foundation.clickable
import androidx.ui.foundation.currentTextStyle
import androidx.ui.foundation.gestures.draggable
import androidx.ui.layout.fillMaxSize
import androidx.ui.layout.preferredSize
import androidx.ui.layout.wrapContentSize

val interactionState = remember { InteractionState() }

val draggable = Modifier.draggable(
    orientation = Orientation.Horizontal,
    interactionState = interactionState
) { /* update some business state here */ }

// Use InteractionState to determine how this component should appear during transient UI states
val (text, color) = when {
    Interaction.Dragged in interactionState -> "Dragged" to Color.Red
    Interaction.Pressed in interactionState -> "Pressed" to Color.Blue
    // Default / baseline state
    else -> "Drag me horizontally, or press me!" to Color.Black
}

Box(
    Modifier
        .fillMaxSize()
        .wrapContentSize()
        .preferredSize(width = 240.dp, height = 80.dp)
) {
    Box(
        Modifier
            .fillMaxSize()
            .clickable(interactionState = interactionState) { /* do nothing */ }
            .plus(draggable),
        border = Border(3.dp, color)
    ) {
        Text(
            text, style = currentTextStyle().copy(textAlign = TextAlign.Center),
            modifier = Modifier.fillMaxSize().wrapContentSize()
        )
    }
}

Summary

Public constructors

InteractionState represents a Set of Interactions present on a given component.

Public methods
Unit
addInteraction(interaction: Interaction, position: Offset? = null)

Adds the provided interaction to this InteractionState.

operator Boolean
contains(interaction: Interaction)

Offset?

Returns the position for a particular Interaction, if there is a position associated with the interaction.

Unit

Removes the provided interaction, if it is present, from this InteractionState.

Inherited extension functions
From androidx.compose
operator T
State<T>.getValue(thisObj: Any?, property: KProperty<*>)

Permits property delegation of vals using by for State.

Properties
Set<Interaction>

Public constructors

<init>

InteractionState()

InteractionState represents a Set of Interactions present on a given component. This allows you to build higher level components comprised of lower level interactions such as clickable and androidx.ui.foundation.gestures.draggable, and react to Interaction changes driven by these components in one place. For Interactions with an associated position, such as Interaction.Pressed, you can retrieve this position by using interactionPositionFor.

Creating an InteractionState and passing it to these lower level interactions will cause a recomposition when there are changes to the state of Interaction, such as when a clickable becomes Interaction.Pressed.

import androidx.compose.remember
import androidx.ui.foundation.Border
import androidx.ui.foundation.Box
import androidx.ui.foundation.InteractionState
import androidx.ui.foundation.Text
import androidx.ui.foundation.clickable
import androidx.ui.foundation.currentTextStyle
import androidx.ui.foundation.gestures.draggable
import androidx.ui.layout.fillMaxSize
import androidx.ui.layout.preferredSize
import androidx.ui.layout.wrapContentSize

val interactionState = remember { InteractionState() }

val draggable = Modifier.draggable(
    orientation = Orientation.Horizontal,
    interactionState = interactionState
) { /* update some business state here */ }

// Use InteractionState to determine how this component should appear during transient UI states
val (text, color) = when {
    Interaction.Dragged in interactionState -> "Dragged" to Color.Red
    Interaction.Pressed in interactionState -> "Pressed" to Color.Blue
    // Default / baseline state
    else -> "Drag me horizontally, or press me!" to Color.Black
}

Box(
    Modifier
        .fillMaxSize()
        .wrapContentSize()
        .preferredSize(width = 240.dp, height = 80.dp)
) {
    Box(
        Modifier
            .fillMaxSize()
            .clickable(interactionState = interactionState) { /* do nothing */ }
            .plus(draggable),
        border = Border(3.dp, color)
    ) {
        Text(
            text, style = currentTextStyle().copy(textAlign = TextAlign.Center),
            modifier = Modifier.fillMaxSize().wrapContentSize()
        )
    }
}

Public methods

addInteraction

fun addInteraction(
    interaction: Interaction,
    position: Offset? = null
): Unit

Adds the provided interaction to this InteractionState. Since InteractionState represents a Set, duplicate interactions will not be added, and hence will not cause a recomposition.

Parameters
interaction: Interaction interaction to add
position: Offset? = null position at which the interaction occurred, if relevant. For example, for Interaction.Pressed, this will be the position of the pointer input that triggered the pressed state.

contains

operator fun contains(interaction: Interaction): Boolean
Return
whether the provided interaction exists inside this InteractionState.

interactionPositionFor

fun interactionPositionFor(interaction: Interaction): Offset?

Returns the position for a particular Interaction, if there is a position associated with the interaction.

Return
position associated with the interaction, or null if the interaction is not present in this state, or there is no associated position with the given interaction.

removeInteraction

fun removeInteraction(interaction: Interaction): Unit

Removes the provided interaction, if it is present, from this InteractionState.

Properties

value

val value: Set<Interaction>