Thomas
01/07/2021, 9:05 PMscaffoldState.snackbarHostState.showSnackbar
you can only set a String, but I would like to set a Text so I can style the text using annotated string builder. When just including a Snackbar without using Scaffold it appears at the top of the screen, which is not what I want (I would like it at the bottom how it is supposed to be). Looks like the Scaffold/SnackbarHost is setting the correct positions.matvei
03/01/2021, 7:57 PMThomas
03/01/2021, 8:11 PM@Composable
fun Sample() {
val scope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()
Scaffold(
scaffoldState = scaffoldState,
snackbarHost = {
SnackbarHost(it) { data ->
if (data.message == "showCustomMessage") {
Snackbar {
Text(
text = buildAnnotatedString {
pushStyle(SpanStyle(fontWeight = FontWeight.Bold))
append(stringResource(R.string.my_custom_message))
pop()
}
)
}
} else {
Snackbar(snackbarData = data)
}
}
},
content = {
Button(
onClick = {
scope.launch {
scaffoldState.snackbarHostState.showSnackbar("showCustomMessage")
}
}
) {
Text("Show snackbar")
}
}
)
}
@Composable
fun Sample() {
val scope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()
Scaffold(
scaffoldState = scaffoldState,
content = {
Button(
onClick = {
scope.launch {
// DOES NOT COMPILE
scaffoldState.snackbarHostState.showSnackbar(
message = {
Text(
text = buildAnnotatedString {
pushStyle(SpanStyle(fontWeight = FontWeight.Bold))
append(stringResource(R.string.my_custom_message))
pop()
}
)
}
)
}
}
) {
Text("Show snackbar")
}
}
)
}
matvei
03/01/2021, 8:23 PMTextStyle
object for itThomas
03/01/2021, 8:50 PMmatvei
03/02/2021, 11:07 AMFilippo Vigani
12/07/2021, 2:14 PMmatvei
12/07/2021, 2:29 PMsnowSnackbar
that accepts the arbitrary instance of the SnackbarData
, so you can create your own custom class MySnackbarWithMetadata: SnackbarData
and then you can do
SnackbarHost(hostState) { data ->
if (data is MySnackbarWithMetadata) {
// show custom snackbar
} else {
// show regular snackbar
}
}
Not the cleanest solution for sure, but it unlocks any custom data propagation, while keeping you mostly in line with specs. Stay tuned 🙂