Skip to content

Instantly share code, notes, and snippets.

@catalinghita8
Created January 3, 2023 18:54
Show Gist options
  • Save catalinghita8/f02435310f7398cc04c6ad2b242099f6 to your computer and use it in GitHub Desktop.
Save catalinghita8/f02435310f7398cc04c6ad2b242099f6 to your computer and use it in GitHub Desktop.
fun CardContainer(
modifier: Modifier,
onClick: () -> Unit = { },
contents: @Composable RowScope.() -> Unit
) {
var size by remember { mutableStateOf(Size.Zero) }
val shader = SweepGradientShader(
center = Offset(size.width / 2, size.height / 2),
colors = listOf(Color.Transparent, Color(0xFFF1C374)),
colorStops = listOf(0.5f, 1f),
)
var hasAnimationFinished by remember { mutableStateOf(false) }
var shakeAnimationEnabled by remember { mutableStateOf(false) }
var offsetFloat by remember { mutableStateOf(0f) }
LaunchedEffect(null) {
delay(100)
offsetFloat = 360f
shakeAnimationEnabled = true
}
val (matrix, brush) = remember(shader, size) {
Matrix().also {
shader.getLocalMatrix(it)
} to ShaderBrush(shader)
}
val angle by animateFloatAsState(
targetValue = offsetFloat,
animationSpec = repeatable(
iterations = 6,
animation = tween(2500, easing = LinearEasing),
)
) {
hasAnimationFinished = true
}
matrix.postRotate(angle, size.width / 2, size.height / 2)
shader.setLocalMatrix(matrix)
val gradientColors = listOf(
colorResource(id = R.color.colorDark).copy(alpha = 0.9f),
Color(0x6064B5F6),
colorResource(id = R.color.colorPrimaryDark).copy(alpha = 0.5f)
)
val borderWidth = 2.5.dp
val borderColor = colorResource(id = R.color.colorPrimaryEnd)
Surface(
shape = RoundedCornerShape(24.dp),
modifier = modifier.background(Color.Transparent),
shadowElevation = 1.dp,
) {
OutlinedButton(
contentPadding = PaddingValues(0.dp),
colors = ButtonDefaults.buttonColors(backgroundColor = Color.Transparent),
modifier = Modifier
.fillMaxSize()
.gradientBackground(
colors = gradientColors,
angle = 45f
)
.onSizeChanged {
size = Size(it.width.toFloat(), it.height.toFloat())
},
border = if (!hasAnimationFinished)
BorderStroke(width = 3.5.dp, brush = brush)
else
BorderStroke(width = borderWidth, color = Color.Transparent),
shape = roundedCornerShape,
onClick = {},
) {
OutlinedButton(
modifier = Modifier.fillMaxSize(),
border = BorderStroke(
borderWidth,
borderColor.copy(alpha = 0.3f)
),
shape = roundedCornerShape,
colors = ButtonDefaults.buttonColors(
backgroundColor = Color.Transparent
),
onClick = onClick
) {
contents()
}
}
}
}
fun Modifier.gradientBackground(colors: List<Color>, angle: Float) = this.then(
Modifier.drawBehind {
val angleRad = angle / 180f * PI
val x = cos(angleRad).toFloat()
val y = sin(angleRad).toFloat()
val radius = sqrt(size.width.pow(2) + size.height.pow(2)) / 2f
val offset = center + Offset(x * radius, y * radius)
val exactOffset = Offset(
x = min(offset.x.coerceAtLeast(0f), size.width),
y = size.height - min(offset.y.coerceAtLeast(0f), size.height)
)
drawRect(
brush = Brush.linearGradient(
colors = colors,
start = Offset(size.width, size.height) - exactOffset,
end = exactOffset
),
size = size
)
}
)
val roundedCornerShape = RoundedCornerShape(24.dp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment