Saturday, October 2, 2021

jetpack compose - How to use ModalDrawer

MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.rememberDrawerState
import androidx.compose.ui.Alignment
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.draw.clip
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            GetScaffold()
        }
    }


    @Composable
    fun GetScaffold(){
        Scaffold(
            topBar = {TopAppBar(
                title = {Text(
                    "Compose - How To Use ModalDrawer",
                    color = Color.White)},
                backgroundColor = Color(0xFF3D0C02)) },
            content = {MainContent()},
            backgroundColor = Color(0xFFEDEAE0)
        )
    }


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

        ModalDrawer(
            drawerState = drawerState,
            drawerShape = MaterialTheme.shapes.small,
            drawerBackgroundColor = Color(0xFFF19CBB),
            drawerContentColor = Color(0XFF1B1811),
            scrimColor = Color(0xFFFAE7B5),
            drawerElevation = 12.dp,
            gesturesEnabled = true,
            modifier = Modifier.clip(
                CutCornerShape(topEnd = 48.dp)
            ),
            drawerContent = {
                Box(
                    Modifier
                        .fillMaxWidth()
                        .padding(12.dp)) {
                    Text(
                        "Go To Home",
                        style = MaterialTheme.typography.h6
                    )
                }
            },
            content = {
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(16.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Button(
                        onClick = { scope.launch{drawerState.open()}}
                    ) {
                        Text(text = if(drawerState.isClosed)
                            "Open Drawer" else "Close Drawer")
                    }
                }
            }
        )
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        //GetScaffold()
    }
}