Skip to content

Instantly share code, notes, and snippets.

@GabriellCosta
Last active August 31, 2023 18:30
Show Gist options
  • Save GabriellCosta/b760f16c46f49db0bf19a9223b9cbe44 to your computer and use it in GitHub Desktop.
Save GabriellCosta/b760f16c46f49db0bf19a9223b9cbe44 to your computer and use it in GitHub Desktop.
Material Navigation Composable dismiss action
package dev.tigrao.myapplication
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.navigation.material.BottomSheetNavigatorSheetState
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
import com.google.accompanist.navigation.material.ModalBottomSheetLayout
import com.google.accompanist.navigation.material.bottomSheet
import com.google.accompanist.navigation.material.rememberBottomSheetNavigator
import dev.tigrao.myapplication.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterialNavigationApi::class, ExperimentalMaterialApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
val bottomSheetNavigator = rememberBottomSheetNavigator()
val navHost = rememberNavController(bottomSheetNavigator)
val sheetState = bottomSheetNavigator.navigatorSheetState
val current = LocalContext.current
ModalBottomSheetLayout(
bottomSheetNavigator = bottomSheetNavigator
) {
NavHost(
navController = navHost,
startDestination = "home",
) {
composable("home") {
Home { dest ->
navHost.navigate(dest)
}
}
composable("greetins/{id}") {
GreetingComposable(text = it.arguments?.getString("id").orEmpty())
}
bottomSheet(route = "sheet") {
OnDismissAction(sheetState) {
Toast.makeText(current, "sheet", Toast.LENGTH_LONG).show()
}
Text("This is a cool bottom sheet!")
}
bottomSheet(route = "sheet2") {
OnDismissAction(sheetState) {
Toast.makeText(current, "sheet2", Toast.LENGTH_LONG).show()
}
Text("This is the other sheet")
}
}
}
}
}
}
}
@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterialNavigationApi::class)
@Composable
fun OnDismissAction(
sheetState: BottomSheetNavigatorSheetState,
action: () -> Unit,
) {
LaunchedEffect(key1 = sheetState.targetValue) {
if (sheetState.targetValue == ModalBottomSheetValue.Hidden) {
action()
}
}
}
@Composable
fun Home(action: (dest: String) -> Unit) {
Column {
Button(onClick = { action("greetins/Gabriel") }) {
Text(text = "Gabriel")
}
Button(onClick = { action("greetins/Andariel") }) {
Text(text = "Andariel")
}
Button(onClick = { action("sheet") }) {
Text(text = "Dialog1")
}
Button(onClick = { action("sheet2") }) {
Text(text = "Dialog2")
}
}
}
@Composable
fun GreetingComposable(text: String) {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting(text)
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
MyApplicationTheme {
Greeting("Android")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment