跳转到内容

ListPreference

ListPreference 是点击后弹出底部单选面板的偏好行,对应 ColorOS 的 COUIListPreference(COUIAlertDialog_BottomAssignment + coui_select_dialog_singlechoice 行)。偏好行行末显示当前选中项文字与 COUI 弹出指示箭头;面板中选中行行末显示主题色对勾。点击面板条目立即提交选择并收起面板。

MultiSelectListPreference 是多选变体,对应 COUIMultiSelectListPreference:面板行行末为复选框,仅在点击确定按钮时提交选择。

引入

kotlin
import io.github.suqi8.coui.kmp.preference.ListPreference
import io.github.suqi8.coui.kmp.preference.ListPreferenceDefaults
import io.github.suqi8.coui.kmp.preference.ListPreferenceEntry
import io.github.suqi8.coui.kmp.preference.MultiSelectListPreference

基本用法

kotlin
val entries = remember {
    listOf(
        ListPreferenceEntry("浅色"),
        ListPreferenceEntry("深色"),
        ListPreferenceEntry("跟随系统", summary = "随系统深色模式切换"),
    )
}
var selectedIndex by remember { mutableIntStateOf(2) }

ListPreference(
    entries = entries,
    selectedIndex = selectedIndex,
    onSelectedIndexChange = { selectedIndex = it },
    title = "主题",
    cancelButtonText = "取消",
)

多选变体

kotlin
var selectedIndices by remember { mutableStateOf(setOf(0, 1)) }

MultiSelectListPreference(
    entries = entries,
    selectedIndices = selectedIndices,
    onSelectedIndicesChange = { selectedIndices = it },
    title = "同步项目",
    confirmButtonText = "确定",
    cancelButtonText = "取消",
)

属性

ListPreferenceEntry

属性类型说明默认值必需
textString条目文字(16sp,中等字重)-
summaryString?条目下方的可选摘要null
enabledBoolean条目是否可选true

ListPreference

属性类型说明默认值必需
entriesList<ListPreferenceEntry>可选条目列表-
selectedIndexInt选中条目索引(-1 表示未选)-
onSelectedIndexChange(Int) -> Unit选中条目时的回调-
titleString偏好行标题-
cancelButtonTextString面板取消按钮文字-
modifierModifier应用于偏好行的修饰符Modifier
titleColorBasicComponentColors标题颜色配置BasicComponentDefaults.titleColor()
summaryString?偏好行摘要null
summaryColorBasicComponentColors摘要颜色配置BasicComponentDefaults.summaryColor()
dialogTitleString选择面板标题title
colorsListPreferenceColors面板行颜色配置ListPreferenceDefaults.listPreferenceColors()
startAction@Composable (() -> Unit)?自定义起始侧内容null
bottomAction@Composable (() -> Unit)?自定义底部内容null
insideMarginPaddingValues行内边距BasicComponentDefaults.InsideMargin
cardListPositionCardListPosition该行在卡片组中的位置,圆角外边缘会获得额外内边距CardListPosition.None
enabledBoolean行是否可点击true
showValueBoolean是否在行末显示当前选中项true
renderInRootScaffoldBoolean是否在根 Scaffold 渲染面板true
onExpandedChange((Boolean) -> Unit)?面板展开 / 收起时的回调null

MultiSelectListPreference

相对 ListPreference 新增 / 不同的属性:

属性类型说明默认值必需
selectedIndicesSet<Int>选中条目的索引集合-
onSelectedIndicesChange(Set<Int>) -> Unit点击确定时返回新选择的回调-
confirmButtonTextString面板确定按钮文字-
checkboxColorsCheckboxColors面板行复选框颜色CheckboxDefaults.checkboxColors()

ListPreferenceDefaults

常量类型默认值COUI 来源
PanelItemMinHeightDp48.dpcoui_delete_alert_dialog_button_height
PanelItemVerticalPaddingDp10.dpalert_dialog_single_list_padding_vertical
PanelItemIndicatorSpacingDp16.dpcoui_dialog_layout_margin_horizontal
PanelItemSummarySpacingDp2.dpcoui_alert_dialog_content_panel_padding_top
CheckIconSizeDp24.dpCOUI 24dp 选择控件
ButtonBarTopPaddingDp6.dpalert_dialog_single_list_last_item_padding_bottom
ButtonBarBottomPaddingDp12.dp库内约定

listPreferenceColors() 工厂

参数类型默认值COUI 角色
itemTextColorColorCOUITheme.colorScheme.onSurfacecouiColorPrimaryNeutral
disabledItemTextColorColorCOUITheme.colorScheme.disabledOnSecondaryVariantcouiColorDisabledNeutral
itemSummaryColorColorCOUITheme.colorScheme.onSurfaceSecondarycouiColorSecondNeutral
disabledItemSummaryColorColorCOUITheme.colorScheme.disabledOnSecondaryVariantcouiColorDisabledNeutral
selectedIndicatorColorColorCOUITheme.colorScheme.primarycouiColorPrimary
disabledSelectedIndicatorColorColorCOUITheme.colorScheme.disabledPrimary-

行为

  • 点击偏好行会弹出以 dialogTitle 为标题的 OverlayBottomSheet 面板,面板存续期间偏好行保持按压态,弹出时触发 context-click 震动。
  • 单选:点击条目立即回调 onSelectedIndexChange 并收起面板;取消按钮或点击外部收起且不做修改(对应 COUIListPreferenceDialogFragment)。
  • 多选:切换条目只更新暂存选择;点击确定按钮才通过 onSelectedIndicesChange 提交,取消 / 点击外部会丢弃修改(对应 COUIMultiSelectListPreferenceDialogFragment)。
  • 相邻面板行之间绘制发丝线分割线(0.33dp,couiColorDivider),最后一行之后不绘制。
  • enabled = false 的条目仍会显示但不可选中,使用禁用态文字颜色。

变更日志

基于 Apache-2.0 许可发布