Last active
March 13, 2022 20:09
-
-
Save pksokolowski/07b799020b67a1498f60f8784736d86e to your computer and use it in GitHub Desktop.
A simplistic, generic spinner view for 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 androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.material.DropdownMenu | |
import androidx.compose.material.DropdownMenuItem | |
import androidx.compose.material.Icon | |
import androidx.compose.material.icons.Icons | |
import androidx.compose.material.icons.filled.ArrowDropDown | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.unit.dp | |
@Composable | |
fun <T> Spinner( | |
items: List<T>, | |
selectedItem: T?, | |
onSelectedItemChanged: (T) -> Unit, | |
drawItem: @Composable (T?) -> Unit, | |
modifier: Modifier = Modifier, | |
dropdownArrowContentDescription: String = "" | |
) { | |
var expanded by remember { mutableStateOf(false) } | |
Box( | |
modifier = modifier | |
.clickable { | |
expanded = !expanded | |
}, | |
contentAlignment = Alignment.CenterEnd | |
) { | |
Row( | |
modifier = Modifier | |
.padding(8.dp), | |
horizontalArrangement = Arrangement.Center, | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
drawItem(selectedItem) | |
Icon( | |
imageVector = Icons.Filled.ArrowDropDown, | |
contentDescription = dropdownArrowContentDescription | |
) | |
DropdownMenu( | |
expanded = expanded, | |
onDismissRequest = { expanded = false } | |
) { | |
items.forEach { item -> | |
DropdownMenuItem( | |
onClick = { | |
expanded = false | |
onSelectedItemChanged(item) | |
} | |
) { | |
drawItem(item) | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment