Code
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Slider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.BlurredEdgeTreatment
import androidx.compose.ui.draw.CacheDrawScope
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.addOutline
import androidx.compose.ui.graphics.drawscope.clipPath
import androidx.compose.ui.graphics.drawscope.clipRect
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.lerp
import theme.Colors.Sky200
import theme.Colors.Sky400
import theme.Colors.Zinc600
import theme.Colors.Zinc900
import theme.Colors.Zinc950
import util.brushedMetal
@Composable
fun MetalSlider() {
Box(
Modifier
.fillMaxSize()
.background(Zinc600),
contentAlignment = Alignment.Center
) {
var value by remember { mutableStateOf(.5f) }
Slider(
value = value,
onValueChange = { value = it },
modifier = Modifier.padding(32.dp),
thumb = {
Box(
Modifier
.size(48.dp, 36.dp)
.shadow(
elevation = 10.dp,
shape = CircleShape,
)
.clip(CircleShape)
.brushedMetal(
highlightRotation = lerp(0f, 180f, value),
highlightAlpha = 1f,
ringCount = 10,
)
.border(
width = 2.dp,
shape = CircleShape,
brush = Brush.verticalGradient(
colors = listOf(
Color.White,
Color.Transparent,
)
)
)
)
},
track = {
Box(
Modifier
.fillMaxWidth()
.border(
width = 4.dp,
shape = CircleShape,
brush = Brush.verticalGradient(
colors = listOf(
Zinc950.copy(alpha = .1f),
Zinc950.copy(alpha = .4f),
)
)
)
.padding(4.dp)
.border(
width = 2.dp,
shape = CircleShape,
brush = Brush.verticalGradient(
colors = listOf(
Zinc950.copy(alpha = .6f),
Zinc950.copy(alpha = .4f),
)
)
)
.padding(2.dp)
.background(
brush = Brush.verticalGradient(
colors = listOf(
Zinc900.copy(alpha = .1f),
Zinc900.copy(alpha = .1f),
)
),
CircleShape
)
.height(10.dp)
) {
Box(
modifier = Modifier
.fillMaxSize()
.drawWithCache {
val path = createPath(CircleShape)
onDrawBehind {
clipPath(path = path) {
clipRect(right = size.width * value) {
drawRect(color = Sky400)
}
}
}
}
)
Box(
modifier = Modifier
.fillMaxSize()
.scale(scaleY = 1.4f, scaleX = 1.03f)
.blur(20.dp, edgeTreatment = BlurredEdgeTreatment.Unbounded)
.drawWithCache {
val path = createPath(CircleShape)
onDrawBehind {
clipPath(path = path) {
clipRect(right = size.width * value) {
drawRect(color = Sky200.copy(alpha = .5f))
}
}
}
}
)
}
}
)
}
}
fun CacheDrawScope.createPath(shape: Shape) = Path().apply {
addOutline(
shape.createOutline(
size = size,
layoutDirection = layoutDirection,
density = Density(density),
)
)
}