【问题标题】:ModalDrawer requires coroutine context to change state hide and show jetpack composeModalDrawer 需要协程上下文来更改状态隐藏和显示 jetpack compose
【发布时间】:2021-02-27 19:32:20
【问题描述】:

我需要使用协程上下文来处理我的代码中的modalState.show()modalState.hide()

@ExperimentalMaterialApi
@Composable
fun ModalBottomSheetLayoutDemo() {
    val modalState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)

    Button(modifier = Modifier.padding(16.dp), onClick = { modalState.show() }) {
        Text("Show Bottom Sheet")
    }

    ModalBottomSheetLayout(sheetState = modalState, sheetContent = {
        Text(
            modifier = Modifier.padding(start = 8.dp, top = 8.dp),
            text = "Title",
            fontWeight = FontWeight.Bold,
            style = typography.h5
        )
        Text(
            modifier = Modifier.padding(start = 8.dp, top = 8.dp),
            text = "Content example right here :)",
            style = typography.body1
        )
        Row(modifier = Modifier.align(Alignment.CenterHorizontally).padding(8.dp)) {
            Button(modifier = Modifier.padding(end = 8.dp), onClick = { modalState.hide() }) {
                Text("Cancel")
            }
            Button(onClick = { modalState.hide() }) {
                Text("Ok")
            }
        }
    }, sheetElevation = 8.dp) {}
}

在它工作之前,现在它需要一个协程上下文,如何在 jetpack compose 中执行上下文?

【问题讨论】:

    标签: android kotlin android-jetpack android-jetpack-compose


    【解决方案1】:

    调用rememberCoroutineScope(),将其保存在一个变量中,然后将其用于launch() 你的show()hide() 调用:

    @ExperimentalMaterialApi
    @Composable
    fun ModalBottomSheetLayoutDemo() {
        val modalState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
        val sillyScope = rememberCoroutineScope()
    
        Button(modifier = Modifier.padding(16.dp), onClick = { sillyScope.launch { modalState.show() } }) {
            Text("Show Bottom Sheet")
        }
    
        ModalBottomSheetLayout(sheetState = modalState, sheetContent = {
            Text(
                modifier = Modifier.padding(start = 8.dp, top = 8.dp),
                text = "Title",
                fontWeight = FontWeight.Bold,
                style = typography.h5
            )
            Text(
                modifier = Modifier.padding(start = 8.dp, top = 8.dp),
                text = "Content example right here :)",
                style = typography.body1
            )
            Row(modifier = Modifier.align(Alignment.CenterHorizontally).padding(8.dp)) {
                Button(modifier = Modifier.padding(end = 8.dp), onClick = { modalState.hide() }) {
                    Text("Cancel")
                }
                Button(onClick = { sillyScope.launch { modalState.hide() } }) {
                    Text("Ok")
                }
            }
        }, sheetElevation = 8.dp) {}
    }
    

    AFAICT,使用suspend调用是因为Compose UI中的动画API使用协程,所以我们需要应用一个合适的CoroutineScoperememberCoroutineScope() 将为您提供一个适用于您的作品的这个分支以某种形式存在的范围。

    【讨论】:

    • 那么,这个作文死后会被取消吗?谢谢
    • @CoffeeBreak:是的。如果用户做了一些导致你的应用重构的事情,这样ModalBottomSheetLayoutDemo() 不再是组合的一部分,你的范围应该被取消。
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多