11package com.kez.picker
22
33import androidx.compose.foundation.background
4+ import androidx.compose.foundation.clickable
45import androidx.compose.foundation.gestures.snapping.rememberSnapFlingBehavior
6+ import androidx.compose.foundation.interaction.MutableInteractionSource
57import androidx.compose.foundation.layout.Box
68import androidx.compose.foundation.layout.PaddingValues
79import androidx.compose.foundation.layout.fillMaxWidth
@@ -20,10 +22,12 @@ import androidx.compose.runtime.LaunchedEffect
2022import androidx.compose.runtime.derivedStateOf
2123import androidx.compose.runtime.getValue
2224import androidx.compose.runtime.remember
25+ import androidx.compose.runtime.rememberCoroutineScope
2326import androidx.compose.runtime.snapshotFlow
2427import androidx.compose.ui.Alignment
2528import androidx.compose.ui.Modifier
2629import androidx.compose.ui.draw.drawWithContent
30+ import androidx.compose.ui.semantics.Role
2731import androidx.compose.ui.graphics.BlendMode
2832import androidx.compose.ui.graphics.Brush
2933import androidx.compose.ui.graphics.Color
@@ -42,6 +46,7 @@ import androidx.compose.ui.unit.lerp
4246import androidx.compose.ui.unit.sp
4347import kotlinx.coroutines.flow.distinctUntilChanged
4448import kotlinx.coroutines.flow.mapNotNull
49+ import kotlinx.coroutines.launch
4550import kotlin.math.abs
4651
4752/* *
@@ -93,6 +98,7 @@ fun <T> Picker(
9398) {
9499 val density = LocalDensity .current
95100 val visibleItemsMiddle = remember { visibleItemsCount / 2 }
101+ val scope = rememberCoroutineScope()
96102
97103 val adjustedItems = if (! isInfinity) {
98104 listOf (null ) + items + listOf (null )
@@ -205,6 +211,22 @@ fun <T> Picker(
205211 modifier = Modifier
206212 .height(itemHeight)
207213 .fillMaxWidth()
214+ .clickable(
215+ enabled = getItem(index) != null ,
216+ role = Role .Button ,
217+ indication = null ,
218+ interactionSource = remember { MutableInteractionSource () },
219+ onClick = {
220+ val currentCenterIndex = listState.firstVisibleItemIndex + visibleItemsMiddle
221+ if (index != currentCenterIndex) {
222+ scope.launch {
223+ val targetIndex = (index - visibleItemsMiddle)
224+ .coerceIn(0 , listScrollCount - 1 )
225+ listState.animateScrollToItem(targetIndex)
226+ }
227+ }
228+ }
229+ )
208230 .padding(itemPadding),
209231 contentAlignment = Alignment .Center
210232 ) {
0 commit comments