Recommended Compose way to have multiple Balloons #491
Unanswered
EternalMelody
asked this question in
Q&A
Replies: 1 comment
|
The idiomatic Compose approach is to avoid Instead, hoist the @Composable
fun MultipleBalloonScreen() {
// Hold references at parent scope so siblings can coordinate
var firstWindow by remember { mutableStateOf<BalloonWindow?>(null) }
var secondWindow by remember { mutableStateOf<BalloonWindow?>(null) }
val builder1 = rememberBalloonBuilder {
setWidth(BalloonSizeSpec.WRAP)
setHeight(BalloonSizeSpec.WRAP)
// ... config for balloon 1
}
val builder2 = rememberBalloonBuilder {
setWidth(BalloonSizeSpec.WRAP)
setHeight(BalloonSizeSpec.WRAP)
// ... config for balloon 2
}
Column {
Balloon(
builder = builder1,
balloonContent = { Text("Edit your profile!") }
) { bw ->
// Capture window on first composition
LaunchedEffect(bw) { firstWindow = bw }
Button(onClick = {
secondWindow?.dismiss() // Dismiss sibling before showing
bw.showAlignTop()
}) {
Text("Show Balloon 1")
}
}
Balloon(
builder = builder2,
balloonContent = { Text("Hello!") }
) { bw ->
LaunchedEffect(bw) { secondWindow = bw }
Button(onClick = {
firstWindow?.dismiss()
bw.showAlignTop()
}) {
Text("Show Balloon 2")
}
}
}
}Why For a walkthrough / onboarding sequence where balloons fire one-after-another, store the windows in a val windows = remember { mutableStateListOf<BalloonWindow?>(null, null, null) }
var step by remember { mutableIntStateOf(0) }
// Show the next balloon
Button(onClick = {
windows.getOrNull(step - 1)?.dismiss()
windows.getOrNull(step)?.showAlignTop()
step++
}) { Text("Next") } |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello Balloon community!
Loving this library so far, that said I have a question. What would be the recommended idiomatic Compose way to have multiple Balloons?
For example I can capture balloonWindow to a variable like this with lateinit var bw:
But I'm not sure if this is recommended. Would appreciate opinions on this :)
All reactions