Skip to content

Instantly share code, notes, and snippets.

@iwata-n
Created October 18, 2021 14:56
Show Gist options
  • Save iwata-n/d4d3d88cfa132572a6b852840fa3a544 to your computer and use it in GitHub Desktop.
Save iwata-n/d4d3d88cfa132572a6b852840fa3a544 to your computer and use it in GitHub Desktop.
左右が角丸になるボタン
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@Composable
fun RoundIconButton(
modifier: Modifier = Modifier,
painter: Painter,
text: String,
onClick: () -> Unit,
) {
Button(
modifier = modifier,
shape = RoundedCornerShape(100),
onClick = onClick
) {
Row(
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
painter = painter,
contentDescription = "post"
)
Text(
modifier = Modifier.padding(start = 6.dp),
text = text,
)
}
}
}
class PreviewSizeProvider : PreviewParameterProvider<Pair<Dp, Dp>> {
override val values: Sequence<Pair<Dp, Dp>>
get() = sequenceOf(
Pair(100.dp, 150.dp),
Pair(150.dp, 100.dp),
Pair(150.dp, 150.dp),
)
}
@Preview
@Composable
fun PreviewPostButton(
@PreviewParameter(PreviewSizeProvider::class) size: Pair<Dp, Dp>,
) {
Column {
RoundIconButton(
modifier = Modifier.size(width = size.first, height = size.second),
painter = rememberVectorPainter(Icons.Filled.Search),
text = "角丸ボタン",
) {}
}
}
@iwata-n
Copy link
Author

iwata-n commented Oct 18, 2021

こんな感じになる

image

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