Página·Grátis
Tela de configurações (Compose)
Tela de configurações do Material 3 com chaves, escolha de tema e controle de tamanho do texto. Não guarda estado: você passa um SettingsState e recebe a cópia alterada. Cada linha é uma área de toque de 56 dp ou mais, lida pelo TalkBack como chave ou opção.
Instalação grátis.
Se o PocketCode estiver instalado no seu Android, isto abre a página no app.
Prévia
SettingsScreen.kt · 190 linhas
// 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",Esta prévia está encurtada. O conteúdo completo vem com a instalação.
Mudanças na 1.0.0
Primeira versão.