0
点赞
收藏
分享

微信扫一扫

Compose中SnackerBar的使用

E_topia 2022-01-09 阅读 29

代码

 val scope = rememberCoroutineScope()
    val scaffoldState = rememberScaffoldState()
    Scaffold(snackbarHost = {
        SnackbarHost(hostState = it) { data ->
//            Snackbar(
//                snackbarData = data,
//                backgroundColor = Color.Green,
//                contentColor = Color.White,
//                shape = CutCornerShape(10.dp)
//            )
            Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
                Snackbar(
                    modifier = Modifier
                        .width(200.dp)
                        .height(50.dp)
                ) {
                    Row(Modifier.fillMaxSize(), verticalAlignment = Alignment.CenterVertically) {
                        Icon(
                            imageVector = Icons.Default.Favorite,
                            contentDescription = "",
                            tint = Color.White
                        )
                        Text(
                            text = data.message,
                            modifier = Modifier.padding(start = 10.dp),
                            style = TextStyle(color = Color.White, fontSize = 20.sp)
                        )
                    }
                }
            }

        }

    },
        scaffoldState = scaffoldState,
        bottomBar = {
            BottomAppBar(backgroundColor = Color.Red) {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceAround
                ) {
                    Text(text = "苹果", color = Color.White)
                    Text(text = "香蕉", color = Color.White)
                    Text(text = "大鸭梨", color = Color.White)
                }
            }
        }
    ) {
        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            Button(onClick = {
                Trace.beginSection("")
                trace("") {
                    (0..30).forEach {
                        println("index:$it")
                    }
                }
                scope.launch {
                    scaffoldState.snackbarHostState.showSnackbar(
                        "哈哈哈哈",
                        duration = SnackbarDuration.Short
                    )
                }
            }) {
                Text(text = "展示Toast")
            }
        }
    }

效果

举报

相关推荐

0 条评论