Composables
Docs Login
Tally

Option Row

Jetpack Compose Component

Download
package com.composables.tally.components

import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.semantics.selected
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.tally.theme.CaretRight
import com.composables.tally.theme.Check
import com.composables.tally.theme.Icons
import com.composables.tally.theme.LocalContentColor
import com.composables.tally.theme.ProvideContentColor
import com.composables.tally.theme.Space
import com.composables.tally.theme.Theme
import com.composeunstyled.UnstyledButton

@Composable
fun OptionRow(
    title: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    description: String? = null,
    icon: ImageVector? = null,
    badge: String? = null,
    selected: Boolean = false,
    enabled: Boolean = true,
    destructive: Boolean = false,
    value: String? = null,
    navigates: Boolean = false,
) {
    UnstyledButton(
        onClick = onClick,
        enabled = enabled,
        modifier =
            modifier.fillMaxWidth().alpha(if (enabled) 1f else 0.45f).semantics {
                this.selected = selected
            },
        contentPadding = PaddingValues(horizontal = Space * 4, vertical = Space * 3),
        contentAlignment = Alignment.CenterStart,
        indication = rememberRippleIndication(color = LocalContentColor.current),
        interactionSource = remember { MutableInteractionSource() },
    ) {
        if (destructive)
            ProvideContentColor(Theme.colors.destructive) {
                OptionLayout(title, description, icon, badge) {}
            }
        else
            OptionLayout(title, description, icon, badge) {
                if (value != null)
                    Text(
                        value,
                        style = Theme.typography.caption,
                        color = Theme.colors.onBackgroundMuted,
                        maxLines = 1,
                    )
                if (selected)
                    Icon(
                        Icons.Check,
                        contentDescription = null,
                        modifier = Modifier.size(20.dp),
                        tint = Theme.colors.primary,
                    )
                if (navigates)
                    Icon(
                        Icons.CaretRight,
                        contentDescription = null,
                        modifier = Modifier.size(16.dp),
                        tint = Theme.colors.onBackgroundMuted,
                    )
            }
    }
}

@Composable
internal fun OptionLayout(
    title: String,
    description: String?,
    icon: ImageVector?,
    badge: String?,
    modifier: Modifier = Modifier,
    trailing: @Composable () -> Unit,
) {
    Row(
        modifier.fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(Space * 3),
    ) {
        if (icon != null) Icon(icon, contentDescription = null, modifier = Modifier.size(20.dp))
        Column(Modifier.weight(1f)) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(Space * 2),
            ) {
                Text(title, style = Theme.typography.label)
                if (badge != null) {
                    Text(
                        badge,
                        style = Theme.typography.caption,
                        color = Theme.colors.onPrimary,
                        modifier =
                            Modifier.clip(Theme.shapes.fullyRounded)
                                .background(Theme.colors.primary)
                                .padding(horizontal = Space * 2, vertical = 1.dp),
                    )
                }
            }
            if (description != null)
                Text(
                    description,
                    style = Theme.typography.caption,
                    color = Theme.colors.onBackgroundMuted,
                )
        }
        trailing()
    }
}

More components from Tally

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.