[英]Jetpack compose snackbar action click is not working
Here is my code:这是我的代码:
@Composable
fun MySnackBar() {
Snackbar(
modifier = Modifier.padding(4.dp),
action = {
TextButton(color = LightBlue,onClick = {
Log.d("TAG", "Action clicked!")
}) {
Text(text = "Remove")
}
}
) {
Text(text = "This is a basic Snackbar with action item")
}
}
In a Scaffold you can use:在脚手架中,您可以使用:
val scope = rememberCoroutineScope()
scope.launch {
val snackbarResult = scaffoldState.snackbarHostState.showSnackbar(
message = "This is a basic Snackbar with action item",
actionLabel = "Remove"
)
when (snackbarResult) {
SnackbarResult.Dismissed -> {}
SnackbarResult.ActionPerformed -> {}
}
}
If you don't have a Scaffold
you can use something like:如果你没有
Scaffold
,你可以使用类似的东西:
val snackState = remember { SnackbarHostState() }
SnackbarHost(hostState = snackState, Modifier){ data ->
Snackbar(
actionColor = Red,
snackbarData = data
)
}
and then use the same code above to show the Snackbar
changing scaffoldState.snackbarHostState
to snackState
.然后使用上面相同的代码来显示
Snackbar
将scaffoldState.snackbarHostState
更改为snackState
。
Button( onClick = { Snackbar(action = {}) { Text("hello") } }`按钮(onClick = { Snackbar(动作 = {}){ 文本(“你好”)} }`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.