Skip to content

Instantly share code, notes, and snippets.

@rajendhirandev
Created September 15, 2022 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajendhirandev/034fe2bf0607dfce66fefc02ae802c0c to your computer and use it in GitHub Desktop.
Save rajendhirandev/034fe2bf0607dfce66fefc02ae802c0c to your computer and use it in GitHub Desktop.
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