Compose Component

OneHandedGestureScrollIndicator

A scroll indicator that transitions to indicate that a scroll gesture is available to the user.

OneHandedGestureTransformingLazyColumnSample

@Sampled
@Composable
fun OneHandedGestureTransformingLazyColumnSample() {
    val backDispatcherOwner = LocalOnBackPressedDispatcherOwner.current
    val onClick =
        remember<() -> Unit> { { backDispatcherOwner?.onBackPressedDispatcher?.onBackPressed() } }
    val scrollState = rememberTransformingLazyColumnState()
    val coroutineScope = rememberCoroutineScope()

    val buttonInteractionSource = remember { MutableInteractionSource() }
    val buttonGestureConfig =
        rememberOneHandedGestureConfiguration(
            action = OneHandedGestureAction.Primary,
            priority = OneHandedGesturePriority.Clickable,
        )
    val buttonIndicatorState = remember { OneHandedGestureClickIndicatorState() }

    val scrollGestureConfig =
        rememberOneHandedGestureConfiguration(
            action = OneHandedGestureAction.Primary,
            priority = OneHandedGesturePriority.Scrollable,
        )
    val scrollIndicatorState =
        remember(scrollGestureConfig) { OneHandedGestureScrollIndicatorState() }

    ScreenScaffold(
        scrollState = scrollState,
        edgeButton = {
            EdgeButton(
                onClick = onClick,
                interactionSource = buttonInteractionSource,
                modifier =
                    if (scrollState.canScrollForward) {
                        Modifier
                    } else {
                        // Apply the one-handed gesture modifier only when the container cannot
                        // scroll further, ensuring the EdgeButton is fully visible and interactive
                        Modifier.oneHandedGesture(
                            gestureConfiguration = buttonGestureConfig,
                            interactionSource = buttonInteractionSource,
                            onGestureLabel = "close",
                            onGestureAvailable = {
                                coroutineScope.launch { buttonIndicatorState.showIndicator() }
                            },
                            onGesture = onClick,
                        )
                    } then
                        Modifier.scrollable(
                            state = scrollState,
                            orientation = Orientation.Vertical,
                            reverseDirection = true,
                            overscrollEffect = rememberOverscrollEffect(),
                        ),
            ) {
                OneHandedGestureClickIndicator(buttonGestureConfig, buttonIndicatorState) {
                    Text("Close")
                }
            }
        },
        scrollIndicator = {
            OneHandedGestureScrollIndicator(
                gestureConfiguration = scrollGestureConfig,
                indicatorState = scrollIndicatorState,
                scrollState = scrollState,
                modifier = Modifier.align(Alignment.CenterEnd),
            )
        },
    ) { contentPadding ->
        TransformingLazyColumn(
            state = scrollState,
            contentPadding = contentPadding,
            modifier =
                Modifier.fillMaxSize()
                    .oneHandedGesture(
                        gestureConfiguration = scrollGestureConfig,
                        onGestureLabel = "scroll",
                        onGestureAvailable = {
                            coroutineScope.launch { scrollIndicatorState.showIndicator() }
                        },
                        onGesture = { OneHandedGestureDefaults.scrollDown(scrollState) },
                    ),
        ) {
            items(10) { Text("Item $it") }
        }
    }
}

OneHandedGestureScalingLazyColumnSample

@Sampled
@Composable
fun OneHandedGestureScalingLazyColumnSample() {
    val backDispatcherOwner = LocalOnBackPressedDispatcherOwner.current
    val onClick =
        remember<() -> Unit> { { backDispatcherOwner?.onBackPressedDispatcher?.onBackPressed() } }
    val scrollState = rememberScalingLazyListState()
    val coroutineScope = rememberCoroutineScope()

    val buttonInteractionSource = remember { MutableInteractionSource() }
    val buttonGestureConfig =
        rememberOneHandedGestureConfiguration(
            action = OneHandedGestureAction.Primary,
            priority = OneHandedGesturePriority.Clickable,
        )
    val buttonIndicatorState = remember { OneHandedGestureClickIndicatorState() }

    val scrollGestureConfig =
        rememberOneHandedGestureConfiguration(
            action = OneHandedGestureAction.Primary,
            priority = OneHandedGesturePriority.Scrollable,
        )
    val scrollIndicatorState =
        remember(scrollGestureConfig) { OneHandedGestureScrollIndicatorState() }

    ScreenScaffold(
        scrollState = scrollState,
        edgeButton = {
            EdgeButton(
                onClick = onClick,
                interactionSource = buttonInteractionSource,
                modifier =
                    if (scrollState.canScrollForward) {
                        Modifier
                    } else {
                        // Apply the one-handed gesture modifier only when the container cannot
                        // scroll further, ensuring the EdgeButton is fully visible and interactive
                        Modifier.oneHandedGesture(
                            gestureConfiguration = buttonGestureConfig,
                            interactionSource = buttonInteractionSource,
                            onGestureLabel = "close",
                            onGestureAvailable = {
                                coroutineScope.launch { buttonIndicatorState.showIndicator() }
                            },
                            onGesture = onClick,
                        )
                    } then
                        Modifier.scrollable(
                            state = scrollState,
                            orientation = Orientation.Vertical,
                            reverseDirection = true,
                            overscrollEffect = rememberOverscrollEffect(),
                        ),
            ) {
                OneHandedGestureClickIndicator(buttonGestureConfig, buttonIndicatorState) {
                    Text("Close")
                }
            }
        },
        scrollIndicator = {
            OneHandedGestureScrollIndicator(
                gestureConfiguration = scrollGestureConfig,
                indicatorState = scrollIndicatorState,
                scrollState = scrollState,
                modifier = Modifier.align(Alignment.CenterEnd),
            )
        },
    ) { contentPadding ->
        ScalingLazyColumn(
            state = scrollState,
            contentPadding = contentPadding,
            modifier =
                Modifier.fillMaxSize()
                    .oneHandedGesture(
                        gestureConfiguration = scrollGestureConfig,
                        onGestureLabel = "scroll",
                        onGestureAvailable = {
                            coroutineScope.launch { scrollIndicatorState.showIndicator() }
                        },
                        onGesture = { OneHandedGestureDefaults.scrollDown(scrollState) },
                    ),
            autoCentering = null,
        ) {
            items(10) { Text("Item $it") }
        }
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: