跳转到内容

WindowSpinner

WindowSpinner 是 Miuix 中的下拉选择器组件,提供了标题、摘要和带有图标、文本的选项列表。它在窗口级别渲染,不需要 Scaffold 宿主,适用于没有或不使用 Scaffold 的场景。

提示

该组件不依赖 Scaffold,可在任意 Composable 作用域中使用。

引入

kotlin
import top.yukonga.miuix.kmp.extra.WindowSpinner
import top.yukonga.miuix.kmp.extra.SpinnerEntry

基本用法

WindowSpinner 组件提供了基础的下拉选择器功能:

kotlin
var selectedIndex by remember { mutableStateOf(0) }
val options = listOf(
    SpinnerEntry(title = "选项 1"),
    SpinnerEntry(title = "选项 2"),
    SpinnerEntry(title = "选项 3"),
)

WindowSpinner(
    title = "下拉选择器",
    items = options,
    selectedIndex = selectedIndex,
    onSelectedIndexChange = { selectedIndex = it }
)

带图标和摘要的选项

kotlin
// 创建一个圆角矩形的 Painter
private class RoundedRectanglePainter(
    private val cornerRadius: Dp = 6.dp
) : Painter() {
    override val intrinsicSize = Size.Unspecified
    override fun DrawScope.onDraw() {
        drawRoundRect(
            color = Color.White,
            size = Size(size.width, size.height),
            cornerRadius = CornerRadius(cornerRadius.toPx(), cornerRadius.toPx())
        )
    }
}

var selectedIndex by remember { mutableStateOf(0) }
val options = listOf(
    SpinnerEntry(
        icon = { Icon(RoundedRectanglePainter(), "Icon", Modifier.padding(end = 12.dp), Color(0xFFFF5B29)) },
        title = "红色主题",
        summary = "活力四射的红色"
    ),
    SpinnerEntry(
        icon = { Icon(RoundedRectanglePainter(), "Icon", Modifier.padding(end = 12.dp), Color(0xFF3482FF)) },
        title = "蓝色主题",
        summary = "沉稳冷静的蓝色"
    ),
)


WindowSpinner(
    title = "菜单",
    items = options,
    selectedIndex = selectedIndex,
    onSelectedIndexChange = { selectedIndex = it }
)

组件状态

禁用状态

kotlin
SuperSpinner(
    title = "禁用选择器",
    summary = "此选择器当前不可用",
    items = listOf(SpinnerEntry(title = "选项 1")),
    selectedIndex = 0,
    onSelectedIndexChange = {},
    enabled = false
)

对话框模式

WindowSpinner 还支持对话框模式,适用于显示较多选项或需要更醒目的选择界面时。通过提供 dialogButtonString 参数即可激活此模式。

kotlin
var selectedIndex by remember { mutableStateOf(0) }
val options = listOf(
    SpinnerEntry(title = "选项 A"),
    SpinnerEntry(title = "选项 B"),
    SpinnerEntry(title = "选项 C")
)

WindowSpinner(
    title = "对话框模式",
    dialogButtonString = "取消",
    items = options,
    selectedIndex = selectedIndex,
    onSelectedIndexChange = { selectedIndex = it },
)

属性

WindowSpinner 属性(下拉列表模式)

属性名类型说明默认值是否必须
itemsList<SpinnerEntry>选项列表-
selectedIndexInt当前选中项的索引-
titleString选择器的标题-
modifierModifier应用于组件的修饰符Modifier
titleColorBasicComponentColors标题文本的颜色配置BasicComponentDefaults.titleColor()
summaryString?摘要说明null
summaryColorBasicComponentColors摘要文本的颜色配置BasicComponentDefaults.summaryColor()
spinnerColorsSpinnerColors选择器的颜色配置SpinnerDefaults.spinnerColors()
startAction@Composable (() -> Unit)?左侧显示的自定义内容null
bottomAction@Composable (() -> Unit)?底部自定义内容null
insideMarginPaddingValues组件内部内容的边距BasicComponentDefaults.InsideMargin
maxHeightDp?弹出框的最大高度null
enabledBoolean组件是否可交互true
showValueBoolean是否显示当前选中值true
onSelectedIndexChange((Int) -> Unit)?选中项变化时的回调-

WindowSpinner 属性 (对话框模式)

属性名类型说明默认值是否必须
itemsList<SpinnerEntry>选项列表-
selectedIndexInt当前选中项的索引-
titleString选择器的标题-
dialogButtonStringString对话框按钮的文本-
modifierModifier应用于组件的修饰符Modifier
popupModifierModifier对话框弹出的修饰符Modifier
titleColorBasicComponentColors标题文本的颜色配置BasicComponentDefaults.titleColor()
summaryString?摘要说明null
summaryColorBasicComponentColors摘要文本的颜色配置BasicComponentDefaults.summaryColor()
spinnerColorsSpinnerColors选择器的颜色配置SpinnerDefaults.dialogSpinnerColors()
startAction@Composable (() -> Unit)?左侧显示的自定义内容null
bottomAction@Composable (() -> Unit)?底部自定义内容null
insideMarginPaddingValues组件内部内容的边距BasicComponentDefaults.InsideMargin
enabledBoolean组件是否可交互true
showValueBoolean是否显示当前选中值true
onSelectedIndexChange((Int) -> Unit)?选中项变化时的回调-

SpinnerEntry 属性

属性名类型说明
icon@Composable ((Modifier) -> Unit)?选项的图标组件
titleString?选项的标题
summaryString?选项的摘要描述

SpinnerColors 属性

属性名类型说明
contentColorColor选项标题颜色
summaryColorColor选项摘要颜色
containerColorColor选项背景颜色
selectedContentColorColor选中项标题颜色
selectedSummaryColorColor选中项摘要颜色
selectedContainerColorColor选中项背景颜色
selectedIndicatorColorColor选中指示图标颜色

变更日志

基于 Apache-2.0 许可发布