Skip to content

Instantly share code, notes, and snippets.

@crakaC
Last active December 11, 2021 16:27
Show Gist options
  • Save crakaC/7ab028bbc979bf62d58f10b02949f63e to your computer and use it in GitHub Desktop.
Save crakaC/7ab028bbc979bf62d58f10b02949f63e to your computer and use it in GitHub Desktop.
DropDownMenuをPreviewする
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
private val MenuElevation = 8.dp
private val MenuVerticalMargin = 48.dp
private val DropdownMenuItemHorizontalPadding = 16.dp
private val DropdownMenuVerticalPadding = 8.dp
private val DropdownMenuItemDefaultMinWidth = 112.dp
private val DropdownMenuItemDefaultMaxWidth = 280.dp
private val DropdownMenuItemDefaultMinHeight = 48.dp
private val DropdownMenuItemContentPadding = PaddingValues(
horizontal = DropdownMenuItemHorizontalPadding,
vertical = 0.dp
)
@Composable
private fun Menu(
modifier: Modifier = Modifier,
content: @Composable () -> Unit
) {
Card(
elevation = MenuElevation
) {
Column(
modifier = modifier
.padding(vertical = DropdownMenuVerticalPadding)
.width(IntrinsicSize.Max)
.verticalScroll(rememberScrollState())
) {
content()
}
}
}
@Composable
private fun Item(
modifier: Modifier = Modifier,
content: @Composable () -> Unit
) {
Row(
modifier
.clickable {}
.fillMaxWidth()
.sizeIn(
minWidth = DropdownMenuItemDefaultMinWidth,
maxWidth = DropdownMenuItemDefaultMaxWidth,
minHeight = DropdownMenuItemDefaultMinHeight
)
.padding(DropdownMenuItemContentPadding),
verticalAlignment = Alignment.CenterVertically
) {
content()
}
}
@Preview
@Composable
private fun Preview() {
val str1 = "short"
val str2 = "Lorem ipsum"
val str3 = "Long Long Very Long Text"
val iconRes = android.R.drawable.ic_delete
Menu {
Item {
Icon(
painter = painterResource(id = iconRes), contentDescription = null,
modifier = Modifier.size(24.dp)
)
Spacer(Modifier.width(8.dp))
Text(text = str2)
Spacer(Modifier.width(8.dp))
Text(text = str2)
}
Item {
Icon(painter = painterResource(id = iconRes), contentDescription = null)
Spacer(Modifier.width(8.dp))
Text(text = str1)
}
Item {
Icon(painter = painterResource(id = iconRes), contentDescription = null)
Spacer(Modifier.width(8.dp))
Text(text = str2)
}
Item {
Icon(
painter = painterResource(id = iconRes), contentDescription = null,
modifier = Modifier.size(24.dp)
)
Spacer(Modifier.width(8.dp))
Text(text = str3)
}
}
}
@crakaC
Copy link
Author

crakaC commented Dec 11, 2021

スクリーンショット 2021-12-12 1 25 55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment