Last active
January 6, 2021 20:46
-
-
Save mitchtabian/0de015d3d6dd4ee74b0f7ebc0e580353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setContent{ | |
val snackbarHostState = remember{SnackbarHostState()} | |
Column { | |
Button( | |
onClick = { | |
lifecycleScope.launch { | |
val time = System.currentTimeMillis() | |
Log.d(TAG, "showing snackbar") | |
snackbarHostState.showSnackbar( | |
message = "Hey look a snackbar", | |
actionLabel = "Hide", | |
duration = SnackbarDuration.Short | |
) | |
Log.d(TAG, "done ${System.currentTimeMillis()-time}") // <-- Never called | |
} | |
} | |
) { | |
Text("Show snackbar") | |
} | |
DecoupledSnackbarDemo(snackbarHostState = snackbarHostState) | |
} | |
} | |
@ExperimentalMaterialApi | |
@Composable | |
fun DecoupledSnackbarDemo( | |
snackbarHostState: SnackbarHostState | |
){ | |
ConstraintLayout(modifier = Modifier.fillMaxSize()) { | |
val snackbar = createRef() | |
SnackbarHost( | |
modifier = Modifier.constrainAs(snackbar) { | |
bottom.linkTo(parent.bottom) | |
start.linkTo(parent.start) | |
end.linkTo(parent.end) | |
}, | |
hostState = snackbarHostState, | |
snackbar = { | |
Snackbar( | |
action = { | |
TextButton( | |
onClick = { | |
snackbarHostState.currentSnackbarData?.dismiss() | |
} | |
){ | |
Text( | |
text = snackbarHostState.currentSnackbarData?.actionLabel?: "", | |
style = TextStyle(color = Color.White) | |
) | |
} | |
} | |
) { | |
Text(snackbarHostState.currentSnackbarData?.message?: "") | |
} | |
} | |
) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment