jetpack compose - Scaffold Snackbar host

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.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.filled.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import kotlinx.coroutines.launch


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


    @Composable
    fun GetScaffold(){
        val scaffoldState: ScaffoldState = rememberScaffoldState(
            snackbarHostState = SnackbarHostState()
        )
        val visibleState:MutableState<Boolean> =
            remember { mutableStateOf(false) }
        val snackBarMessage:MutableState<String> =
            remember { mutableStateOf("This is a snackbar") }

        Scaffold(
            scaffoldState = scaffoldState,
            topBar = {
                TopAppBar(
                    title = { Text(
                        text = "Compose - Scaffold Snackbar host"
                    )},
                    backgroundColor = Color(0xFFC0E8D5),
                )
            },
            content = {MainContent(visibleState,snackBarMessage)},
            backgroundColor = Color(0xFFEDEAE0),
            snackbarHost = {
                if (visibleState.value){
                    Snackbar(
                        action = {
                            TextButton(
                                onClick = { visibleState.value = false }
                            ) {
                                Text(text = "Hide")
                            }
                        },
                        content = {
                            Text(text = snackBarMessage.value)
                        },
                        backgroundColor = Color(0xFFFF9966)
                    )
                }
            }
        )
    }


    @Composable
    fun MainContent(
        visibleState:MutableState<Boolean>,
        snackbarMessage:MutableState<String>
    ){
        val scope = rememberCoroutineScope()
        var counter by remember { mutableStateOf(0)}

        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ){
            Button(onClick = {
                scope.launch{
                    counter++
                    visibleState.value = true
                    snackbarMessage.value =
                        "Hello, i am a snackbar ($counter)"
                }
            }) {
                Text(text = "Show Snackbar")
            }
        }
    }
}
More android jetpack compose tutorials