跳转到内容
页面·免费

设置页面(Compose)

Material 3 设置页面,包含开关、主题选择和文字大小滑块。它本身不保存状态:你传入 SettingsState,就会拿回修改后的副本。每一行都是至少 56 dp 的单个触摸区域,TalkBack 会把它读作开关或单选按钮。

免费安装。

如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。

预览

SettingsScreen.kt · 190 行
// Settings screen for Jetpack Compose and Material 3, no extra libraries.
// It holds no state of its own: keep SettingsState in your ViewModel and save it where you like.
//
// val state by viewModel.settings.collectAsState()
// SettingsScreen(state = state, onStateChange = viewModel::update, onAboutClick = { /* open About */ }, appVersion = "1.4.2")
//
// Each row is one touch target of at least 56 dp, and TalkBack reads it as a switch or a radio button.
// Move the texts to strings.xml before you translate the app.

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.selection.toggleable
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Slider
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt

enum class ThemeChoice(val label: String) {
    System("Use the system setting"),
    Light("Light"),
    Dark("Dark"),
}

data class SettingsState(
    val pushNotifications: Boolean = true,
    val weeklySummary: Boolean = false,
    val theme: ThemeChoice = ThemeChoice.System,
    /** Multiplier for your app's text size, from 0.8 to 1.4. */
    val textScale: Float = 1f,
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(
    state: SettingsState,
    onStateChange: (SettingsState) -> Unit,
    onAboutClick: () -> Unit,
    modifier: Modifier = Modifier,
    appVersion: String = "",
) {
    Scaffold(
        modifier = modifier,
        topBar = { TopAppBar(title = { Text("Settings") }) },
    ) { padding ->
        Column(
            Modifier
                .fillMaxSize()
                .padding(padding)
                .verticalScroll(rememberScrollState()),
        ) {
            SectionHeader("Notifications")
            SwitchRow(
                title = "Push notifications",

此预览已截短。安装后可获得完整内容。

1.0.0 的变更

首个版本。

相关