Monday, October 4, 2021

jetpack compose - Scaffold with Drawer

MainActivity.kt

package com.cfsuman.jetpackcompose

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


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


    @Composable
    fun GetScaffold(){
        val scaffoldState: ScaffoldState = rememberScaffoldState(
            rememberDrawerState(DrawerValue.Closed)
        )

        val clickedItem:MutableState<String> = remember {
            mutableStateOf("")}

        Scaffold(
            scaffoldState = scaffoldState,
            topBar = { TopAppBarContent(scaffoldState) },
            content = {MainContent(clickedItem)},
            backgroundColor = Color(0xFFEDEAE0),
            drawerContent = {DrawerContent(scaffoldState,clickedItem)},
            drawerBackgroundColor = Color(0xFFF0F8FF),
            drawerScrimColor = Color(0XFFFAE7B5),
            drawerContentColor = Color(0xFF0048BA),
            drawerShape = CutCornerShape(bottomEnd = 45.dp)
        )
    }


    @Composable
    fun TopAppBarContent(scaffoldState: ScaffoldState) {
        val scope = rememberCoroutineScope()
        TopAppBar(
            title = { Text(text = "Compose - Scaffold + Drawer")},
            backgroundColor = Color(0xFFC0E8D5),

            navigationIcon = {
                IconButton(onClick = {
                    // do something here
                    scope.launch{
                        scaffoldState.drawerState.open()
                    }
                }) {
                    Icon(
                        Icons.Filled.Menu,
                        contentDescription = "Localized description"
                    )
                }
            }
        )
    }


    @Composable
    fun DrawerContent(
        scaffoldState: ScaffoldState,
        clickedItem: MutableState<String>){
        val scope = rememberCoroutineScope()

        Column(modifier = Modifier
            .fillMaxWidth()
            .padding(12.dp),
        ){
            Row() {
                Text(
                    text = "APP Title",
                    style = MaterialTheme.typography.h5
                )
            }
            Spacer(modifier = Modifier.requiredHeight(12.dp))
            Row(modifier = Modifier
                .clickable {
                    scope.launch {
                        scaffoldState.drawerState.close()
                        clickedItem.value = "ThumbUp Clicked"
                    }
                }.padding(8.dp)) {
                Icon(Icons.Filled.ThumbUp, contentDescription = "")
                Spacer(modifier = Modifier.requiredWidth(12.dp))
                Text(text ="ThumbUp")
            }

            Row(modifier = Modifier
                .clickable {
                    scope.launch {
                        scaffoldState.drawerState.close()
                        clickedItem.value = "Favorite Clicked"
                    }
                }.padding(8.dp)) {
                Icon(Icons.Filled.Favorite, contentDescription = "")
                Spacer(modifier = Modifier.requiredWidth(12.dp))
                Text(text = "Favorite")
            }
        }
    }


    @Composable
    fun MainContent(clickedItem:MutableState<String>){
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ){
            Text(
                text = "${clickedItem.value}",
                style = MaterialTheme.typography.h6
            )
        }
    }


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