Skip to content

Commit e987dca

Browse files
fix #1
1 parent ff9c642 commit e987dca

File tree

1 file changed

+74
-61
lines changed

1 file changed

+74
-61
lines changed

slider/src/main/java/com/smarttoolfactory/slider/SliderWithLabel.kt

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.smarttoolfactory.slider
22

33
import androidx.compose.foundation.BorderStroke
44
import androidx.compose.foundation.Canvas
5-
import androidx.compose.foundation.background
65
import androidx.compose.foundation.layout.Box
6+
import androidx.compose.foundation.layout.BoxWithConstraints
77
import androidx.compose.foundation.layout.Column
88
import androidx.compose.foundation.layout.offset
99
import androidx.compose.runtime.*
@@ -79,73 +79,86 @@ fun SliderWithLabel(
7979
yOffset: Dp = 0.dp,
8080
label: @Composable () -> Unit = {}
8181
) {
82-
Column(modifier = Modifier) {
82+
BoxWithConstraints {
8383

84-
val yOffsetInt = with(LocalDensity.current) {
85-
yOffset.toPx().toInt()
86-
}
84+
val maxWidth = constraints.maxWidth.toFloat()
85+
Column {
8786

88-
var labelOffset by remember { mutableStateOf(Offset.Zero) }
89-
var labelWidth by remember { mutableStateOf(0) }
87+
val yOffsetInt = with(LocalDensity.current) {
88+
yOffset.toPx().toInt()
89+
}
9090

91-
if (labelPosition == LabelPosition.Top) {
92-
Box(Modifier
93-
.offset {
94-
IntOffset(labelOffset.x.toInt() - labelWidth / 2, yOffsetInt)
95-
}
96-
.onSizeChanged {
97-
labelWidth = it.width
98-
}
99-
) {
100-
label()
91+
var labelOffset by remember {
92+
mutableStateOf(
93+
Offset(
94+
x = scale(
95+
valueRange.start, valueRange.endInclusive, value, 0f, maxWidth
96+
),
97+
y = 0f
98+
)
99+
)
101100
}
102-
ColorfulSlider(
103-
modifier = modifier,
104-
value = value,
105-
onValueChange = { value, offset ->
106-
labelOffset = offset
107-
onValueChange(value)
108-
},
109-
enabled = enabled,
110-
valueRange = valueRange,
111-
steps = steps,
112-
onValueChangeFinished = onValueChangeFinished,
113-
trackHeight = trackHeight,
114-
thumbRadius = thumbRadius,
115-
borderStroke = borderStroke,
116-
drawInactiveTrack = drawInactiveTrack,
117-
coerceThumbInTrack = coerceThumbInTrack,
118-
colors = colors
119-
)
120-
} else {
121-
ColorfulSlider(
122-
modifier = modifier,
123-
value = value,
124-
onValueChange = { value, offset ->
125-
labelOffset = offset
126-
onValueChange(value)
127-
},
128-
enabled = enabled,
129-
valueRange = valueRange,
130-
steps = steps,
131-
onValueChangeFinished = onValueChangeFinished,
132-
trackHeight = trackHeight,
133-
thumbRadius = thumbRadius,
134-
borderStroke = borderStroke,
135-
drawInactiveTrack = drawInactiveTrack,
136-
coerceThumbInTrack = coerceThumbInTrack,
137-
colors = colors
138-
)
101+
var labelWidth by remember { mutableStateOf(0) }
139102

140-
Box(Modifier
141-
.offset {
142-
IntOffset(labelOffset.x.toInt() - labelWidth / 2, yOffsetInt)
103+
if (labelPosition == LabelPosition.Top) {
104+
Box(Modifier
105+
.offset {
106+
IntOffset(labelOffset.x.toInt() - labelWidth / 2, yOffsetInt)
107+
}
108+
.onSizeChanged {
109+
labelWidth = it.width
110+
}
111+
) {
112+
label()
143113
}
144-
.onSizeChanged {
145-
labelWidth = it.width
114+
ColorfulSlider(
115+
modifier = modifier,
116+
value = value,
117+
onValueChange = { value, offset ->
118+
labelOffset = offset
119+
onValueChange(value)
120+
},
121+
enabled = enabled,
122+
valueRange = valueRange,
123+
steps = steps,
124+
onValueChangeFinished = onValueChangeFinished,
125+
trackHeight = trackHeight,
126+
thumbRadius = thumbRadius,
127+
borderStroke = borderStroke,
128+
drawInactiveTrack = drawInactiveTrack,
129+
coerceThumbInTrack = coerceThumbInTrack,
130+
colors = colors
131+
)
132+
} else {
133+
ColorfulSlider(
134+
modifier = modifier,
135+
value = value,
136+
onValueChange = { value, offset ->
137+
labelOffset = offset
138+
onValueChange(value)
139+
},
140+
enabled = enabled,
141+
valueRange = valueRange,
142+
steps = steps,
143+
onValueChangeFinished = onValueChangeFinished,
144+
trackHeight = trackHeight,
145+
thumbRadius = thumbRadius,
146+
borderStroke = borderStroke,
147+
drawInactiveTrack = drawInactiveTrack,
148+
coerceThumbInTrack = coerceThumbInTrack,
149+
colors = colors
150+
)
151+
152+
Box(Modifier
153+
.offset {
154+
IntOffset(labelOffset.x.toInt() - labelWidth / 2, yOffsetInt)
155+
}
156+
.onSizeChanged {
157+
labelWidth = it.width
158+
}
159+
) {
160+
label()
146161
}
147-
) {
148-
label()
149162
}
150163
}
151164
}

0 commit comments

Comments
 (0)