Skip to content

Instantly share code, notes, and snippets.

@surajsau
Created December 7, 2020 15:58
Show Gist options
  • Save surajsau/06aaa979978998419c1012be977f0846 to your computer and use it in GitHub Desktop.
Save surajsau/06aaa979978998419c1012be977f0846 to your computer and use it in GitHub Desktop.
@Composable
fun NeomorphButton(
modifier: Modifier = Modifier
) {
// [1]
val buttonState = remember { mutableStateOf(ButtonState.IDLE) }
// [2]
val fromState = if(buttonState.value == ButtonState.IDLE) {
ButtonState.PRESSED
} else {
ButtonState.IDLE
}
val state = transition(
definition = transitionDefinition {
state(ButtonState.IDLE) { this[buttonStateProgress] = 0f }
state(ButtonState.PRESSED) { this[buttonStateProgress] = 1f }
// [3]
transition(ButtonState.IDLE to ButtonState.PRESSED,
ButtonState.PRESSED to ButtonState.IDLE) {
buttonStateProgress using tween()
}
},
// [4]
initState = fromState,
toState = buttonState.value
)
CircleNeomorph(
state = state,
modifier = modifier
// [5]
.pressIndicatorGestureFilter(
onStart = { buttonState.value = ButtonState.PRESSED },
onStop = { buttonState.value = ButtonState.IDLE }
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment