Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sateless and State hoisting Sample
//Stateful Compose
@Composable
fun MyCounterStateful() {
var vStart by remember { mutableStateOf(5) }
var vEnd by remember { mutableStateOf(7) }
var noOfTimes by remember { mutableStateOf(0) }
ComposePracticesTheme {
Column(modifier = Modifier.padding(15.dp)) {
Text(text = "Welcome to the Counter")
ButtonCounter(noOfTimes) {
noOfTimes++
}
AnimatedVisibility(visible = noOfTimes in vStart..vEnd + 1) {
CustomTextAvailable(vStart, vEnd)
if (noOfTimes == vEnd + 1) {
vStart = noOfTimes + 5
vEnd = vStart + 2
}
}
}
}
}
//Stateless Compose Custom Text
@Composable
fun CustomTextAvailable(sValue: Int, eValue: Int) {
Text(
text = "I'm Visible between $sValue to $eValue",
modifier = Modifier
)
}
//Stateless Compose Button Counter
@Composable
fun ButtonCounter(count: Int, clickAction: () -> Unit) {
Column {
Button(onClick = {
clickAction()
}) {
val msg = if (count == 0)
"Click Me"
else
"I've Clicked by $count times"
Text(text = msg)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment