FilterChip

Functions summary

Unit
@ExperimentalMaterialApi
@Composable
FilterChip(
    selected: Boolean,
    onClick: () -> Unit,
    modifier: Modifier,
    enabled: Boolean,
    interactionSource: MutableInteractionSource?,
    shape: Shape,
    border: BorderStroke?,
    colors: SelectableChipColors,
    leadingIcon: (@Composable () -> Unit)?,
    selectedIcon: (@Composable () -> Unit)?,
    trailingIcon: (@Composable () -> Unit)?,
    content: @Composable RowScope.() -> Unit
)

Material Design filter chip

Cmn

Functions

FilterChip

@ExperimentalMaterialApi
@Composable
fun FilterChip(
    selected: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource? = null,
    shape: Shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50)),
    border: BorderStroke? = null,
    colors: SelectableChipColors = ChipDefaults.filterChipColors(),
    leadingIcon: (@Composable () -> Unit)? = null,
    selectedIcon: (@Composable () -> Unit)? = null,
    trailingIcon: (@Composable () -> Unit)? = null,
    content: @Composable RowScope.() -> Unit
): Unit

Material Design filter chip

Filter chips use tags or descriptive words to filter a collection. They are a good alternative to toggle buttons or checkboxes.

import androidx.compose.foundation.layout.requiredSize
import androidx.compose.material.Chip
import androidx.compose.material.ChipDefaults
import androidx.compose.material.FilterChip
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Done
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier

val state = remember { mutableStateOf(false) }
FilterChip(
    selected = state.value,
    onClick = { state.value = !state.value },
    selectedIcon = {
        Icon(
            imageVector = Icons.Filled.Done,
            contentDescription = "Localized Description",
            modifier = Modifier.requiredSize(ChipDefaults.SelectedIconSize),
        )
    },
) {
    Text("Filter chip")
}

A filter chip with leading icon and selected icon looks like:

import androidx.compose.foundation.layout.requiredSize
import androidx.compose.material.Chip
import androidx.compose.material.ChipDefaults
import androidx.compose.material.FilterChip
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Done
import androidx.compose.material.icons.filled.Home
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier

val state = remember { mutableStateOf(false) }
FilterChip(
    selected = state.value,
    onClick = { state.value = !state.value },
    leadingIcon = {
        Icon(
            imageVector = Icons.Filled.Home,
            contentDescription = "Localized description",
            modifier = Modifier.requiredSize(ChipDefaults.LeadingIconSize),
        )
    },
    selectedIcon = {
        Icon(
            imageVector = Icons.Filled.Done,
            contentDescription = "Localized Description",
            modifier = Modifier.requiredSize(ChipDefaults.SelectedIconSize),
        )
    },
) {
    Text("Filter chip")
}

You can create an outlined filter chip using ChipDefaults.outlinedFilterChipColors and ChipDefaults.outlinedBorder

import androidx.compose.foundation.layout.requiredSize
import androidx.compose.material.Chip
import androidx.compose.material.ChipDefaults
import androidx.compose.material.FilterChip
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Done
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier

val state = remember { mutableStateOf(false) }
FilterChip(
    selected = state.value,
    onClick = { state.value = !state.value },
    border = ChipDefaults.outlinedBorder,
    colors = ChipDefaults.outlinedFilterChipColors(),
    selectedIcon = {
        Icon(
            imageVector = Icons.Filled.Done,
            contentDescription = "Localized Description",
            modifier = Modifier.requiredSize(ChipDefaults.SelectedIconSize),
        )
    },
) {
    Text("Filter chip")
}
Parameters
selected: Boolean

boolean state for this chip: either it is selected or not

onClick: () -> Unit

will be called when the user clicks the chip

modifier: Modifier = Modifier

Modifier to be applied to the chip

enabled: Boolean = true

controls the enabled state of the chip. When false, this chip will not be clickable

interactionSource: MutableInteractionSource? = null

an optional hoisted MutableInteractionSource for observing and emitting Interactions for this chip. You can use this to change the chip's appearance or preview the chip in different states. Note that if null is provided, interactions will still happen internally.

shape: Shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50))

defines the chip's shape as well as its shadow

border: BorderStroke? = null

border to draw around the chip

colors: SelectableChipColors = ChipDefaults.filterChipColors()

SelectableChipColors that will be used to resolve the background and content color for this chip in different states. See ChipDefaults.filterChipColors.

leadingIcon: (@Composable () -> Unit)? = null

Optional icon at the start of the chip, preceding the content text.

selectedIcon: (@Composable () -> Unit)? = null

Icon used to indicate a chip's selected state, it is commonly a androidx.compose.material.icons.Icons.Filled.Done. By default, if a leading icon is also provided, the leading icon will be obscured by a circle overlay and then the selected icon.

trailingIcon: (@Composable () -> Unit)? = null

Optional icon at the end of the chip, following the content text. Filter chips commonly do not display any trailing icon.

content: @Composable RowScope.() -> Unit

the content of this chip