Skip to content

RadioButtonPreference

RadioButtonPreference is a radio button component in COUI that provides a title, summary, and radio button control. It supports click interactions and is commonly used in single-select settings and selection lists.

Import

kotlin
import io.github.suqi8.coui.kmp.preference.RadioButtonPreference

Basic Usage

RadioButtonPreference is typically used within a mutually exclusive group:

kotlin
var selectedIndex by remember { mutableIntStateOf(0) }

Card {
    RadioButtonPreference(
        title = "Option A",
        selected = selectedIndex == 0,
        onClick = { selectedIndex = 0 }
    )
    RadioButtonPreference(
        title = "Option B",
        selected = selectedIndex == 1,
        onClick = { selectedIndex = 1 }
    )
    RadioButtonPreference(
        title = "Option C",
        selected = selectedIndex == 2,
        onClick = { selectedIndex = 2 }
    )
}

RadioButton with Summary

kotlin
var selectedIndex by remember { mutableIntStateOf(0) }

RadioButtonPreference(
    title = "Option A",
    summary = "Description for option A",
    selected = selectedIndex == 0,
    onClick = { selectedIndex = 0 }
)

Component States

Disabled State

kotlin
RadioButtonPreference(
    title = "Disabled RadioButton",
    summary = "This radio button is currently unavailable",
    selected = true,
    onClick = {},
    enabled = false
)

Properties

RadioButtonPreference Properties

Property NameTypeDescriptionDefault ValueRequired
titleStringTitle of the radio button item-Yes
selectedBooleanRadio button selected state-Yes
onClick(() -> Unit)?Callback when radio button is clicked-Yes
modifierModifierModifier applied to componentModifierNo
titleColorBasicComponentColorsTitle text color configurationBasicComponentDefaults.titleColor()No
summaryString?Summary descriptionnullNo
summaryColorBasicComponentColorsSummary text color configurationBasicComponentDefaults.summaryColor()No
radioButtonColorsRadioButtonColorsRadioButton control color configurationRadioButtonDefaults.radioButtonColors()No
startAction@Composable (() -> Unit)?Custom start contentnullNo
endActions@Composable (RowScope.() -> Unit)?Custom end contentnullNo
radioButtonLocationRadioButtonLocationLocation of the radio button controlRadioButtonLocation.StartNo
bottomAction@Composable (() -> Unit)?Custom bottom contentnullNo
insideMarginPaddingValuesInternal content paddingBasicComponentDefaults.InsideMarginNo
cardListPositionCardListPositionRow position inside its card group; rounded outer edges gain extra paddingCardListPosition.NoneNo
holdDownStateBooleanWhether the component is held downfalseNo
enabledBooleanWhether component is interactivetrueNo

RadioButtonLocation Options

OptionDescription
StartThe radio button is placed at the start (default)
EndThe radio button is placed at the end

Advanced Usage

Radio Button at the End

kotlin
var selectedIndex by remember { mutableIntStateOf(0) }

RadioButtonPreference(
    title = "Option A",
    selected = selectedIndex == 0,
    onClick = { selectedIndex = 0 },
    radioButtonLocation = RadioButtonLocation.End
)

Custom Colors

kotlin
var selected by remember { mutableStateOf(false) }

RadioButtonPreference(
    title = "Custom Colors",
    titleColor = BasicComponentDefaults.titleColor(
        color = COUITheme.colorScheme.primary
    ),
    summary = "RadioButton with custom colors",
    summaryColor = BasicComponentDefaults.summaryColor(
        color = COUITheme.colorScheme.secondary
    ),
    selected = selected,
    onClick = { selected = !selected },
    radioButtonColors = RadioButtonDefaults.radioButtonColors(
        selectedColor = Color.Red
    )
)

Using with Dialog

kotlin
var showDialog by remember { mutableStateOf(false) }
var selectedTheme by remember { mutableIntStateOf(0) }
val themes = listOf("Light", "Dark", "System")

Scaffold {
    ArrowPreference(
        title = "Theme Settings",
        onClick = { showDialog = true },
        holdDownState = showDialog
    )

    OverlayDialog(
        title = "Theme Settings",
        show = showDialog,
        onDismissRequest = { showDialog = false }
    ) {
        Card {
            themes.forEachIndexed { index, theme ->
                RadioButtonPreference(
                    title = theme,
                    selected = selectedTheme == index,
                    onClick = { selectedTheme = index }
                )
            }
        }

        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            modifier = Modifier.padding(top = 12.dp)
        ) {
            TextButton(
                text = "Cancel",
                onClick = { showDialog = false },
                modifier = Modifier.weight(1f)
            )
            Spacer(Modifier.width(16.dp))
            TextButton(
                text = "Confirm",
                onClick = { showDialog = false },
                modifier = Modifier.weight(1f),
                colors = ButtonDefaults.textButtonColorsPrimary()
            )
        }
    }
}

Changelog

Released under the Apache-2.0 License