在 Jetpack compose 中使用 ModalDrawer 从右到左打开导航抽屉

Open navigation drawer from right to left with ModalDrawer in Jetpack compose

我一直在尝试在 Jetpack compose 中实现导航抽屉。以下代码显示了一种简单的方法:

@Composable
fun ModalDrawerSample() {
    val drawerState = rememberDrawerState(DrawerValue.Closed)
    val scope = rememberCoroutineScope()

    ModalDrawer(
        drawerState = drawerState,
        drawerContent = {
            Column {
                Text("Text in Drawer")
                Button(onClick = {
                    scope.launch {
                        drawerState.close()
                    }
                }) {
                    Text("Close Drawer")
                }
            }
        },
        content = {
            Column {
                Text("Text in Bodycontext")
                Button(onClick = {

                    scope.launch {
                        drawerState.open()
                    }

                }) {
                    Text("Click to open")
                }
            }
        }
    )
}

但是如何让导航抽屉从右向左打开?

您可以使用 LocalLayoutDirection 提供 LayoutDirection.Rtl 值。

类似于:

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl ) {
    ModalDrawer(
        drawerState = drawerState,
        drawerContent = { /* ...*/ },
        content = { /* ..*/ }
    )
}