We can have Navigation Drawer in JetpackCompose by using Scaffold
as below
Scaffold(
drawerContent = { Text(text ="Drawer") }
) {}
I want to make the width of the drawer smaller. How can I do so?
We can have Navigation Drawer in JetpackCompose by using Scaffold
as below
Scaffold(
drawerContent = { Text(text ="Drawer") }
) {}
I want to make the width of the drawer smaller. How can I do so?
Here is an example drawing a Scaffold
custom shape.
1. Spec exact size.
Scaffold(
drawerShape = NavShape(400.dp, 0f)
)
2. Spec size by percent (half screen).
Scaffold(
drawerShape = NavShape(0.dp, 0.5f)
)
Custom shape class
class NavShape(
private val widthOffset: Dp,
private val scale: Float
) : Shape {
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density
): Outline {
return Outline.Rectangle(
Rect(
Offset.Zero,
Offset(
size.width * scale + with(density) { widthOffset.toPx() },
size.height
)
)
)
}
}
I accomplished that by:
#1 - Setting to the Drawer's content Modifier a max width:
@Composable
fun MyDrawerContent(){
Column(Modifier.fillMaxHeight().width(300.dp)) {
...
#2 - Setting the Scafold's drawer colors to Transparent + no Elevation
Scaffold(
drawerBackgroundColor = Color.Transparent,
drawerContentColor = Color.Transparent,
drawerElevation = 0.dp,
drawerContent = {
MyDrawerContent()
},
...
Note: Only problem I faced is if you touch on the transparent area the drawer does not closes as it used to. Siding it to close works perfectly though. It's a workaround...
You can modify the shape (including width and height) using drawerShape
parameter in Scaffold method.
So for instance
Scaffold(
scaffoldState = scaffoldState,
drawerContent = { Text("Drawer content") },
drawerShape = customShape(),
content = {inner padding -> /* Body*/}
)
Then your customShape function
fun customShape() = object : Shape {
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density
): Outline {
return Outline.Rectangle(Rect(0f,0f,100f /* width */, 131f /* height */))
}
}
You can also use Outline.Rounded to get rounded corners
Have you tried adding a modifier inside your drawer composable ?
Something like this: