Ahmed Sellami
02/16/2024, 4:17 PM@Composable
fun ActionButton(
modifier: Modifier = Modifier,
vector: ImageVector? = null,
text: String,
) {
Column(
modifier = modifier
.background(MaterialTheme.colorScheme.background)
.padding(16.dp)
.wrapContentSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Box(
modifier = Modifier
.size(100.dp)
.clip(MaterialTheme.shapes.large)
.background(MaterialTheme.colorScheme.background)
.shadow(
elevation = 2.dp,
shape = MaterialTheme.shapes.large
),
contentAlignment = Alignment.Center,
) {
vector?.let {
Icon(
modifier = Modifier.size(42.dp),
imageVector = vector,
contentDescription = text,
tint = MaterialTheme.colorScheme.primary,
)
}
}
Text(
text,
style = MaterialTheme.typography.titleSmall,
modifier = Modifier.padding(top = 8.dp)
)
}
}
Preview code:
@Preview(showBackground = true)
@Composable
fun ActionButtonPreview() {
JetpackComposeTestTheme {
ActionButton(
modifier = Modifier,
vector = Icons.Outlined.ShoppingCart,
text = "Purchase"
)
}
}
Usage in activity:
setContent {
JetpackComposeTestTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
ActionButton(
modifier = Modifier,
vector = Icons.Outlined.ShoppingCart,
text = "Purchase"
)
}
}
}
The theme is nothing custom, it's just generated when I created the project, but here is the code anyway:
@Composable
fun JetpackComposeTestTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit
) {
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
darkTheme -> DarkColorScheme
else -> LightColorScheme
}
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
}
}
MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
}
Kirill Grouchnikov
02/16/2024, 10:55 PMAhmed Sellami
02/17/2024, 2:08 PM