Skip to content

Instantly share code, notes, and snippets.

@kevinah95
Created October 8, 2025 00:11
Show Gist options
  • Save kevinah95/635a9addc6ee33924236cbee22b1a0dd to your computer and use it in GitHub Desktop.
Save kevinah95/635a9addc6ee33924236cbee22b1a0dd to your computer and use it in GitHub Desktop.
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Mic
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.NavigationRail
import androidx.compose.material3.NavigationRailItem
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Slider
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Switch
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun App() {
var showSheet by remember { mutableStateOf(false) }
val sheetState = rememberModalBottomSheetState()
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
var selectedIndex by remember { mutableIntStateOf(0) }
val scrollState = rememberScrollState()
Scaffold(
snackbarHost = { SnackbarHost(snackbarHostState) },
topBar = {
TopAppBar(
title = { Text("Compose Components") },
navigationIcon = {
IconButton(onClick = { /*TODO*/ }) {
Icon(Icons.Default.Menu, contentDescription = "Menu")
}
}
)
},
bottomBar = {
BottomAppBarExample()
},
floatingActionButton = {
FloatingActionButton(onClick = {
scope.launch {
snackbarHostState.showSnackbar("Snackbar!")
}
}) {
Icon(Icons.Default.Add, contentDescription = "Add")
}
}
) { innerPadding ->
Row(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
) {
NavigationRailExample()
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(scrollState)
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
// Layout Components
Text("Layouts", style = MaterialTheme.typography.titleLarge)
RowAndColumnExample()
BoxExample()
// Selection Controls
Text("Selection Controls", style = MaterialTheme.typography.titleLarge)
CheckboxExample()
RadioButtonExample()
SwitchExample()
// Input
Text("Input", style = MaterialTheme.typography.titleLarge)
TextFieldExample()
ExposedDropdownMenuBoxExample()
SliderExample()
// Containers
Text("Containers", style = MaterialTheme.typography.titleLarge)
CardExample()
TextButton(onClick = { showSheet = true }) {
Text("Show Bottom Sheet")
}
// Navigation
Text("Navigation", style = MaterialTheme.typography.titleLarge)
TabRowExample()
// Other
Text("Other UI Elements", style = MaterialTheme.typography.titleLarge)
ProgressIndicators()
AlertDialogExample()
}
if (showSheet) {
ModalBottomSheet(
onDismissRequest = { showSheet = false },
sheetState = sheetState
) {
Column(modifier = Modifier.padding(16.dp)) {
Text("This is a modal bottom sheet")
Spacer(modifier = Modifier.height(16.dp))
TextButton(
onClick = {
scope.launch { sheetState.hide() }.invokeOnCompletion {
if (!sheetState.isVisible) {
showSheet = false
}
}
}
) {
Text("Hide Bottom Sheet")
}
}
}
}
}
}
}
@Composable
fun RowAndColumnExample() {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("Column Child 1")
Text("Column Child 2")
Row {
Text("Row Child 1")
Spacer(modifier = Modifier.size(8.dp))
Text("Row Child 2")
}
}
}
@Composable
fun BoxExample() {
Box(
modifier = Modifier
.size(100.dp)
.background(Color.LightGray),
contentAlignment = Alignment.Center
) {
Box(
modifier = Modifier
.size(50.dp)
.background(Color.Blue)
)
Text("On Top")
}
}
@Composable
fun CheckboxExample() {
var checked by remember { mutableStateOf(true) }
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = checked,
onCheckedChange = { checked = it }
)
Text("A Checkbox")
}
}
@Composable
fun RadioButtonExample() {
val options = listOf("Option A", "Option B")
var selectedOption by remember { mutableStateOf(options[0]) }
Row(verticalAlignment = Alignment.CenterVertically) {
options.forEach { text ->
Row(verticalAlignment = Alignment.CenterVertically) {
RadioButton(
selected = (text == selectedOption),
onClick = { selectedOption = text }
)
Text(text = text, modifier = Modifier.padding(start = 4.dp))
}
}
}
}
@Composable
fun SwitchExample() {
var checked by remember { mutableStateOf(true) }
Row(verticalAlignment = Alignment.CenterVertically) {
Switch(
checked = checked,
onCheckedChange = { checked = it }
)
Text("A Switch", modifier = Modifier.padding(start = 8.dp))
}
}
@Composable
fun TextFieldExample() {
var text by remember { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it },
label = { Text("Enter Text") }
)
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ExposedDropdownMenuBoxExample() {
val options = arrayOf("Option 1", "Option 2", "Option 3")
var expanded by remember { mutableStateOf(false) }
var selectedText by remember { mutableStateOf(options[0]) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = !expanded }
) {
TextField(
modifier = Modifier.menuAnchor(),
readOnly = true,
value = selectedText,
onValueChange = {},
label = { Text("Label") },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
colors = ExposedDropdownMenuDefaults.textFieldColors(),
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
options.forEach { selectionOption ->
androidx.compose.material3.DropdownMenuItem(
text = { Text(selectionOption) },
onClick = {
selectedText = selectionOption
expanded = false
}
)
}
}
}
}
@Composable
fun CardExample() {
Card(
modifier = Modifier.fillMaxWidth(),
elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
) {
Column(modifier = Modifier.padding(16.dp)) {
Text("This is a Card")
}
}
}
@Composable
fun BottomAppBarExample() {
BottomAppBar(
actions = {
IconButton(onClick = { /* do something */ }) {
Icon(Icons.Filled.Check, contentDescription = "Localized description")
}
IconButton(onClick = { /* do something */ }) {
Icon(
Icons.Filled.Edit,
contentDescription = "Localized description",
)
}
IconButton(onClick = { /* do something */ }) {
Icon(
Icons.Filled.Mic,
contentDescription = "Localized description",
)
}
}
)
}
@Composable
fun NavigationRailExample() {
var selectedItem by remember { mutableIntStateOf(0) }
val items = listOf("Home", "Search", "Settings")
val icons = listOf(Icons.Filled.Favorite, Icons.Filled.Favorite, Icons.Filled.Favorite)
NavigationRail {
items.forEachIndexed { index, item ->
NavigationRailItem(
icon = { Icon(icons[index], contentDescription = item) },
label = { Text(item) },
selected = selectedItem == index,
onClick = { selectedItem = index }
)
}
}
}
@Composable
fun TabRowExample() {
var tabIndex by remember { mutableIntStateOf(0) }
val tabs = listOf("Home", "About", "Settings")
Column {
TabRow(selectedTabIndex = tabIndex) {
tabs.forEachIndexed { index, title ->
Tab(text = { Text(title) },
selected = tabIndex == index,
onClick = { tabIndex = index }
)
}
}
Text(
modifier = Modifier.align(Alignment.CenterHorizontally),
text = "${tabs[tabIndex]} content"
)
}
}
@Composable
fun SliderExample() {
var sliderPosition by remember { mutableFloatStateOf(0f) }
Column {
Slider(
value = sliderPosition,
onValueChange = { sliderPosition = it }
)
Text(text = "Slider position: $sliderPosition")
}
}
@Composable
fun ProgressIndicators() {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
CircularProgressIndicator()
Spacer(Modifier.height(8.dp))
LinearProgressIndicator()
}
}
@Composable
fun AlertDialogExample() {
var openDialog by remember { mutableStateOf(false) }
TextButton(onClick = { openDialog = true }) {
Text("Show Dialog")
}
if (openDialog) {
androidx.compose.material3.AlertDialog(
onDismissRequest = { openDialog = false },
title = { Text(text = "Dialog Title") },
text = { Text("Here is some text for the dialog.") },
confirmButton = {
TextButton(
onClick = { openDialog = false }
) {
Text("Confirm")
}
},
dismissButton = {
TextButton(
onClick = { openDialog = false }
) {
Text("Dismiss")
}
}
)
}
}
// In composeApp/build.gradle.kts
kotlin {
sourceSets {
commonMain.dependencies {
// ... other dependencies
implementation(compose.materialIconsExtended) // Add this line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment