|
16 | 16 |
|
17 | 17 | package com.google.android.fhir.datacapture.views.compose |
18 | 18 |
|
19 | | -import androidx.compose.foundation.interaction.MutableInteractionSource |
20 | | -import androidx.compose.foundation.interaction.collectIsDraggedAsState |
21 | | -import androidx.compose.foundation.layout.Box |
| 19 | +import androidx.compose.foundation.layout.Column |
| 20 | +import androidx.compose.foundation.layout.Spacer |
22 | 21 | import androidx.compose.foundation.layout.fillMaxWidth |
23 | | -import androidx.compose.foundation.layout.height |
24 | | -import androidx.compose.foundation.layout.padding |
25 | 22 | import androidx.compose.material3.MaterialTheme |
26 | 23 | import androidx.compose.material3.Slider |
27 | | -import androidx.compose.material3.Surface |
28 | 24 | import androidx.compose.material3.Text |
29 | 25 | import androidx.compose.runtime.Composable |
30 | 26 | import androidx.compose.runtime.getValue |
31 | 27 | import androidx.compose.runtime.mutableFloatStateOf |
32 | 28 | import androidx.compose.runtime.remember |
33 | 29 | import androidx.compose.runtime.setValue |
34 | 30 | import androidx.compose.ui.Modifier |
35 | | -import androidx.compose.ui.layout.layout |
36 | 31 | import androidx.compose.ui.platform.testTag |
37 | | -import androidx.compose.ui.unit.dp |
| 32 | +import androidx.compose.ui.text.font.FontWeight |
38 | 33 | import kotlin.math.roundToInt |
39 | 34 |
|
40 | 35 | @Composable |
41 | 36 | fun SliderItem( |
42 | | - startPosition: Float, |
| 37 | + position: Float, |
43 | 38 | steps: Int, |
44 | 39 | valueRange: ClosedFloatingPointRange<Float>, |
45 | 40 | enabled: Boolean, |
46 | 41 | onPositionChanged: (Float) -> Unit, |
47 | 42 | ) { |
48 | | - var sliderPosition by remember(startPosition) { mutableFloatStateOf(startPosition) } |
49 | | - val interactionSource = |
50 | | - remember(startPosition, steps, valueRange, enabled) { MutableInteractionSource() } |
51 | | - val isDragged by interactionSource.collectIsDraggedAsState() |
52 | | - |
53 | | - Box { |
54 | | - if (isDragged) { |
55 | | - Surface( |
56 | | - modifier = |
57 | | - Modifier.layout { measurable, constraints -> |
58 | | - val placeable = measurable.measure(constraints) |
59 | | - val sliderWidth = constraints.maxWidth |
60 | | - val sliderValueNormalized = |
61 | | - (sliderPosition - valueRange.start) / (valueRange.endInclusive - valueRange.start) |
62 | | - val textX = |
63 | | - (sliderWidth * sliderValueNormalized - placeable.width / 2).coerceIn( |
64 | | - 0f, |
65 | | - (sliderWidth - placeable.width).toFloat(), |
66 | | - ) |
67 | | - val bottomPadding = 16.dp.toPx().roundToInt() |
68 | | - layout(placeable.width, placeable.height - bottomPadding) { |
69 | | - placeable.placeRelative(textX.roundToInt(), -bottomPadding) |
70 | | - } |
71 | | - }, |
72 | | - shape = MaterialTheme.shapes.small, |
73 | | - color = MaterialTheme.colorScheme.primary, |
74 | | - ) { |
75 | | - Text( |
76 | | - text = sliderPosition.roundToInt().toString(), |
77 | | - color = MaterialTheme.colorScheme.onPrimary, |
78 | | - modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp), |
79 | | - ) |
80 | | - } |
81 | | - } |
| 43 | + var sliderPosition by remember(position) { mutableFloatStateOf(position) } |
82 | 44 |
|
| 45 | + Column { |
83 | 46 | Slider( |
84 | 47 | value = sliderPosition, |
85 | 48 | onValueChange = { sliderPosition = it }, |
86 | 49 | onValueChangeFinished = { onPositionChanged(sliderPosition) }, |
87 | 50 | steps = steps, |
88 | 51 | valueRange = valueRange, |
89 | | - interactionSource = interactionSource, |
90 | | - modifier = Modifier.padding(top = 16.dp).fillMaxWidth().testTag(SLIDER_TAG), |
| 52 | + modifier = Modifier.fillMaxWidth().testTag(SLIDER_TAG), |
91 | 53 | enabled = enabled, |
92 | 54 | ) |
| 55 | + Spacer(modifier = Modifier) |
| 56 | + Text( |
| 57 | + text = sliderPosition.roundToInt().toString(), |
| 58 | + style = MaterialTheme.typography.displaySmall, |
| 59 | + fontWeight = FontWeight.ExtraLight, |
| 60 | + ) |
93 | 61 | } |
94 | 62 | } |
95 | 63 |
|
|
0 commit comments