Created
January 5, 2023 18:57
-
-
Save dandvl/993204cebbb8b8b49a8c3d4299599a77 to your computer and use it in GitHub Desktop.
SnackBar with Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.material.Scaffold | |
import androidx.compose.material.SnackbarResult | |
import androidx.compose.material.rememberScaffoldState | |
import androidx.compose.material3.Button | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.rememberCoroutineScope | |
import androidx.compose.ui.Modifier | |
import kotlinx.coroutines.launch | |
@Composable | |
fun SnackbarDemo() { | |
val scaffoldState = rememberScaffoldState() | |
val coroutineScope = rememberCoroutineScope() | |
Scaffold( | |
modifier = Modifier, | |
scaffoldState = scaffoldState, | |
content = { padding -> | |
Column( | |
modifier = Modifier.padding(padding) | |
){ | |
Button( | |
onClick = { | |
coroutineScope.launch { | |
val snackbarResult = scaffoldState.snackbarHostState.showSnackbar( | |
message = "Showing snack bar", | |
actionLabel = "Action Label!" | |
) | |
when (snackbarResult) { | |
SnackbarResult.Dismissed -> Log.d("RMC","snackbar dismissed") | |
SnackbarResult.ActionPerformed -> Log.d("RMC","action taken") | |
} | |
} | |
}, | |
){ | |
Text(text = "text") | |
} | |
} | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment