Skip to Content
Folienvorschau
Dauer
10 Minuten
Ziel
Farben und Schriften aus dem Theme statt fester Werte

Themen

  • colorScheme und typography
  • Card
  • Dark Mode und Dynamic Color
Klicken für Tastaturnavigation · F für Vollbild
Präsentation starten · 10 Minuten ↗

Material 3

Zum Schluss der Feinschliff: Die Karte im Wetter-Screen hat bisher eine feste Farbe (Color(0xFFBBDEFB)) und feste Schriftgrößen. Im Dark Mode ist ihre Schrift auf dem hellen Hintergrund kaum lesbar, weil das Scaffold die Textfarbe an den dunklen Hintergrund anpasst.

Theme statt fester Werte

Material 3 liefert ein Theme mit Farben und Schriften und fertige Bausteine wie Card.

  • MaterialTheme.colorScheme: Farben statt Color(0xFF…)
  • MaterialTheme.typography: Schriftgrößen statt fontSize = …sp
  • Card: Fläche mit Hintergrund und abgerundeten Ecken
Card( modifier = modifier.fillMaxWidth().padding(16.dp), colors = CardDefaults.cardColors( containerColor = MaterialTheme.colorScheme.primaryContainer, ), ) { Column(/* ... */) { Text(text = city.name, style = MaterialTheme.typography.headlineMedium) // ... } }

In CurrentWeatherCard ersetzt die Card die selbst gebaute Box mit background. Den Innenabstand bekommt jetzt die Column. Die neue Funktion CurrentWeatherCard:

ui/WeatherScreen.kt
@Composable fun CurrentWeatherCard( city: City, weather: CurrentWeather, useCelsius: Boolean, modifier: Modifier = Modifier, ) { Card( modifier = modifier .fillMaxWidth() .padding(16.dp), colors = CardDefaults.cardColors( containerColor = MaterialTheme.colorScheme.primaryContainer, ), ) { Column( modifier = Modifier .fillMaxWidth() .padding(24.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { Text(text = city.name, style = MaterialTheme.typography.headlineMedium) Text(text = weather.condition.emoji(), fontSize = 72.sp) Text( text = formatTemperature(weather.temperature, useCelsius), style = MaterialTheme.typography.displayLarge, ) Spacer(modifier = Modifier.height(16.dp)) Row(modifier = Modifier.fillMaxWidth()) { Text( text = "Wind: ${weather.windSpeed.roundToInt()} km/h", style = MaterialTheme.typography.bodyLarge, ) Spacer(modifier = Modifier.weight(1f)) Text(text = weather.condition.label(), style = MaterialTheme.typography.bodyLarge) } } } }

Neue Imports: Card, CardDefaults und MaterialTheme aus androidx.compose.material3. Die Imports von background, RoundedCornerShape und Color werden nicht mehr gebraucht; Optimize Imports (⌃ ⌥ O bzw. Ctrl+Alt+O) räumt sie auf.

Nur das Emoji behält eine feste Größe, weil es im Theme keinen passenden Schriftstil dafür gibt. Die Card setzt passend zur Hintergrundfarbe auch die Textfarbe (onPrimaryContainer).

Dark Mode und Dynamic Color

Weil Farben und Schriften aus dem Theme kommen, funktioniert der Dark Mode automatisch.

@Preview(showBackground = true) @Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES, name = "Dark Mode") @Composable fun WeatherContentPreview() { /* ... */ }

Zwei @Preview-Annotationen an derselben Funktion erzeugen zwei Previews, hier eine helle und eine dunkle. Configuration kommt aus android.content.res.Configuration.

Die ganze Datei nach diesem Schritt:

Code: ui/WeatherScreen.kt (schritt-10)

ui/WeatherScreen.kt
package com.example.weatherandroidtif24.ui import android.content.res.Configuration import androidx.compose.foundation.layout.Box 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.height import androidx.compose.foundation.layout.padding import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel import com.example.weatherandroidtif24.data.fakeCities import com.example.weatherandroidtif24.data.fakeCurrentWeather import com.example.weatherandroidtif24.model.City import com.example.weatherandroidtif24.model.CurrentWeather import com.example.weatherandroidtif24.ui.theme.WeatherAndroidTIF24Theme import kotlin.math.roundToInt // Stateful: holt den Zustand aus dem ViewModel @Composable fun WeatherScreen( city: City, modifier: Modifier = Modifier, viewModel: WeatherViewModel = viewModel(), ) { val uiState by viewModel.uiState.collectAsStateWithLifecycle() WeatherContent( city = city, uiState = uiState, modifier = modifier, ) } // Stateless: zeigt nur an, was es bekommt (dadurch auch in der Preview nutzbar) @OptIn(ExperimentalMaterial3Api::class) @Composable fun WeatherContent( city: City, uiState: WeatherUiState, modifier: Modifier = Modifier, ) { // UI-Element-State: bleibt im Composable, gehört nicht ins ViewModel var useCelsius by rememberSaveable { mutableStateOf(true) } Scaffold( modifier = modifier, topBar = { TopAppBar( title = { Text(text = city.name) }, actions = { UnitToggle(useCelsius = useCelsius, onToggle = { useCelsius = !useCelsius }) }, ) }, ) { innerPadding -> when (uiState) { WeatherUiState.Loading -> CenteredBox(Modifier.padding(innerPadding)) { CircularProgressIndicator() } is WeatherUiState.Success -> CurrentWeatherCard( city = uiState.city, weather = uiState.weather, useCelsius = useCelsius, modifier = Modifier.padding(innerPadding), ) is WeatherUiState.Error -> CenteredBox(Modifier.padding(innerPadding)) { Text(text = uiState.message) } } } } @Composable fun CenteredBox(modifier: Modifier = Modifier, content: @Composable () -> Unit) { Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) { content() } } @Composable fun UnitToggle(useCelsius: Boolean, onToggle: () -> Unit, modifier: Modifier = Modifier) { TextButton(onClick = onToggle, modifier = modifier) { Text(text = if (useCelsius) "°C" else "°F") } } @Composable fun CurrentWeatherCard( city: City, weather: CurrentWeather, useCelsius: Boolean, modifier: Modifier = Modifier, ) { Card( modifier = modifier .fillMaxWidth() .padding(16.dp), colors = CardDefaults.cardColors( containerColor = MaterialTheme.colorScheme.primaryContainer, ), ) { Column( modifier = Modifier .fillMaxWidth() .padding(24.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { Text(text = city.name, style = MaterialTheme.typography.headlineMedium) Text(text = weather.condition.emoji(), fontSize = 72.sp) Text( text = formatTemperature(weather.temperature, useCelsius), style = MaterialTheme.typography.displayLarge, ) Spacer(modifier = Modifier.height(16.dp)) Row(modifier = Modifier.fillMaxWidth()) { Text( text = "Wind: ${weather.windSpeed.roundToInt()} km/h", style = MaterialTheme.typography.bodyLarge, ) Spacer(modifier = Modifier.weight(1f)) Text(text = weather.condition.label(), style = MaterialTheme.typography.bodyLarge) } } } } @Preview(showBackground = true) @Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES, name = "Dark Mode") @Composable fun WeatherContentPreview() { val city = fakeCities.first() WeatherAndroidTIF24Theme { WeatherContent( city = city, uiState = WeatherUiState.Success(city, fakeCurrentWeather.getValue(city.name)), ) } } @Preview(showBackground = true, name = "Loading") @Composable fun WeatherContentLoadingPreview() { WeatherAndroidTIF24Theme { WeatherContent( city = fakeCities.first(), uiState = WeatherUiState.Loading, ) } } @Preview(showBackground = true, name = "Error") @Composable fun WeatherContentErrorPreview() { WeatherAndroidTIF24Theme { WeatherContent( city = fakeCities.first(), uiState = WeatherUiState.Error("Keine Verbindung zum Server"), ) } }

Ab Android 12 passt Dynamic Color die Farben sogar an das Hintergrundbild des Geräts an. Die Vorlage schaltet das in ui/theme/Theme.kt bereits ein (dynamicColor: Boolean = true).

Code: schritt-10 · Änderungen 

Weiterführendes Material