从ModalNavigationDrawer 的源代码中,最大宽度设置在修饰符中,并且抽屉内容的列设置为填充最大大小,因此默认情况下它将采用NavigationDrawerTokens.ContainerWidth 的值作为宽度。
但是这里有一个解决方法,您可以做的是您需要将drawerContainerColor 设置为Color.Transparent 并在drawerContent 内放置您所需大小的另一列或框。
您可以使用requiredWidth 修饰符来固定所需的宽度,也可以根据最小和最大宽度使用sizeIn 修饰符。
ModalNavigationDrawer(
drawerContent = {
Column(
modifier = Modifier
.requiredWidth(250.dp)
.fillMaxHeight()
.background(Color.White, RoundedCornerShape(topEnd = 16.dp, bottomEnd = 16.dp))
) {
//Drawer Content
}
},
drawerContainerColor = Color.Transparent
) {
//Main Content
}
通过点击更新以关闭抽屉,点击背景 -
如果你想对 Spacer 的可点击产生涟漪效应,只需删除interactionSource 和indication 参数。
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val scope = rememberCoroutineScope()
ModalNavigationDrawer(
drawerState = drawerState,
drawerContent = {
Row(modifier = Modifier.fillMaxWidth()) {
Column(
modifier = Modifier
.requiredWidth(250.dp)
.fillMaxHeight()
.background(
Color.White,
RoundedCornerShape(topEnd = 16.dp, bottomEnd = 16.dp)
)
) {
//Drawer Content
}
Spacer(
modifier = Modifier
.fillMaxSize()
.clickable(
interactionSource = MutableInteractionSource(),
indication = null
) {
scope.launch {
if (drawerState.isOpen) {
drawerState.close()
}
}
},
)
}
},
drawerContainerColor = Color.Transparent
) {
//Main Content
}