I'll give an example of the problem, let's say I have two buttons within the Box
@Preview
@Composable
fun testView() {
Box(modifier = Modifier
.size(200.dp)
) {
Button(onClick = {
Log.e("HERE", "First btn")
}) {
Text(text = "Btn1")
}
Button(modifier = Modifier
onClick = {
Log.e("HERE", "Second btn")
}) {
Text(text = "Btn2")
}
}
}
When I run the app I see Btn2
(which is actually on the top of the Btn1
) and when I click in the log I see Second btn
as expected.
Let's say I need to hide Btn2
so the user can click Btn1
, in order to do this in old way I would add View.GONE
and it would work, however in JetpackCompose (as far as I understood) there is only alpha
option, so I try to set it to 0f
like this
...
Button(modifier = Modifier
.alpha(0f),
onClick = {
Log.e("HERE", "Second btn")
}) {
Text(text = "Btn2")
}
...
When I run the app I see (as expected) Btn1
on the screen, however, when I click I see in the log Second btn
.
So, it is obvious that alpha
is changing the visibility but the view itself is still there.
The question is there an alternative for View.GONE
as it was before?
CodePudding user response:
The way to do that would be not to output the second button if you would want it GONE.
if( some_condition) {
Button(modifier = Modifier
onClick = {
Log.e("HERE", "Second btn")
}) {
Text(text = "Btn2")
}
}
If some_condition is false, it won't output the Button, and it won't appear.
CodePudding user response:
You need to create an object that triggers recombination,Recombination is triggered when state changes.
var isSplash by mutableStateOf(true)
if (isSplash) {
SplashView()
isSplash = false
} else {
Main()
//...
}