Flutter-Themes und Styling
In diesem Abschnitt wird die Theme-Konfiguration in Flutter vorgestellt, einschließlich Material-Design-Themes, benutzerdefinierter Themes und Dunkelmodus.
ThemeData - App-Theme
Mit ThemeData können Sie die Theme-Farben, Schriftarten, Stile und mehr der App einheitlich konfigurieren.
Beispiel: Konfigurieren des App-Themes
void main() {
runApp(
MaterialApp(
theme: ThemeData(
// Primärfarbe
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.light,
),
// Material 3 verwenden
useMaterial3: true,
// Schriftart
fontFamily: 'Roboto',
// AppBar-Theme
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
),
// Button-Theme
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
),
),
// Eingabefeld-Theme
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
filled: true,
),
),
home: const MyHomePage(),
),
);
}
runApp(
MaterialApp(
theme: ThemeData(
// Primärfarbe
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.light,
),
// Material 3 verwenden
useMaterial3: true,
// Schriftart
fontFamily: 'Roboto',
// AppBar-Theme
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
),
// Button-Theme
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
),
),
// Eingabefeld-Theme
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
filled: true,
),
),
home: const MyHomePage(),
),
);
}
Theme.of - Verwendung des Themes
Verwende Theme.of(context) in einem Widget, um das aktuelle Theme abzurufen.
Beispiel: Verwenden von Theme-Farben
class ThemedButton extends StatelessWidget {
const ThemedButton({super.key});
@override
Widget build(BuildContext context) {
// Theme abrufen
final theme = Theme.of(context);
return Column(
children: [
// Theme-Farbe verwenden
Container(
color: theme.colorScheme.primary,
padding: const EdgeInsets.all(16),
child: Text(
'Primärfarbe',
style: TextStyle(color: theme.colorScheme.onPrimary),
),
),
const SizedBox(height: 16),
// Sekundärfarbe verwenden
Container(
color: theme.colorScheme.secondary,
padding: const EdgeInsets.all(16),
child: Text(
'Sekundärfarbe',
style: TextStyle(color: theme.colorScheme.onSecondary),
),
),
const SizedBox(height: 16),
// Fehlerfarbe verwenden
Container(
color: theme.colorScheme.error,
padding: const EdgeInsets.all(16),
child: Text(
'Fehlerfarbe',
style: TextStyle(color: theme.colorScheme.onError),
),
),
],
);
}
}
const ThemedButton({super.key});
@override
Widget build(BuildContext context) {
// Theme abrufen
final theme = Theme.of(context);
return Column(
children: [
// Theme-Farbe verwenden
Container(
color: theme.colorScheme.primary,
padding: const EdgeInsets.all(16),
child: Text(
'Primärfarbe',
style: TextStyle(color: theme.colorScheme.onPrimary),
),
),
const SizedBox(height: 16),
// Sekundärfarbe verwenden
Container(
color: theme.colorScheme.secondary,
padding: const EdgeInsets.all(16),
child: Text(
'Sekundärfarbe',
style: TextStyle(color: theme.colorScheme.onSecondary),
),
),
const SizedBox(height: 16),
// Fehlerfarbe verwenden
Container(
color: theme.colorScheme.error,
padding: const EdgeInsets.all(16),
child: Text(
'Fehlerfarbe',
style: TextStyle(color: theme.colorScheme.onError),
),
),
],
);
}
}
Dunkelmodus
Beispiel: Unterstützung für den Dunkelmodus
void main() {
runApp(
MaterialApp(
theme: ThemeData(
// Helles Theme
brightness: Brightness.light,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
),
darkTheme: ThemeData(
// Dunkles Theme
brightness: Brightness.dark,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.dark,
),
),
// Beide aktivieren (optional)
themeMode: ThemeMode.system, // Basierend auf Systemeinstellungen
// themeMode: ThemeMode.light // Hellmodus erzwingen
// themeMode: ThemeMode.dark // Dunkelmodus erzwingen
home: const ThemeTogglePage(),
),
);
}
// Theme umschalten
class ThemeTogglePage extends StatelessWidget {
const ThemeTogglePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Theme wechseln')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Aktuelles Theme'),
const SizedBox(height: 20),
// Theme-Umschaltbutton
ElevatedButton(
onPressed: () {
final currentMode = Theme.of(context).brightness;
if (currentMode == Brightness.light) {
// Zum Dunkelmodus wechseln
Theme.of(context).setThemeMode(ThemeMode.dark);
} else {
// Zum Hellmodus wechseln
Theme.of(context).setThemeMode(ThemeMode.light);
}
},
child: const Text('Theme umschalten'),
),
],
),
),
);
}
}
runApp(
MaterialApp(
theme: ThemeData(
// Helles Theme
brightness: Brightness.light,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
),
darkTheme: ThemeData(
// Dunkles Theme
brightness: Brightness.dark,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.dark,
),
),
// Beide aktivieren (optional)
themeMode: ThemeMode.system, // Basierend auf Systemeinstellungen
// themeMode: ThemeMode.light // Hellmodus erzwingen
// themeMode: ThemeMode.dark // Dunkelmodus erzwingen
home: const ThemeTogglePage(),
),
);
}
// Theme umschalten
class ThemeTogglePage extends StatelessWidget {
const ThemeTogglePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Theme wechseln')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Aktuelles Theme'),
const SizedBox(height: 20),
// Theme-Umschaltbutton
ElevatedButton(
onPressed: () {
final currentMode = Theme.of(context).brightness;
if (currentMode == Brightness.light) {
// Zum Dunkelmodus wechseln
Theme.of(context).setThemeMode(ThemeMode.dark);
} else {
// Zum Hellmodus wechseln
Theme.of(context).setThemeMode(ThemeMode.light);
}
},
child: const Text('Theme umschalten'),
),
],
),
),
);
}
}
Benutzerdefinierte Style-Erweiterungen
Beispiel: Benutzerdefiniertes TextTheme
ThemeData(
textTheme: const TextTheme(
// Anzeigetext (Überschrift)
displayLarge: TextStyle(fontSize: 57, fontWeight: FontWeight.w400),
displayMedium: TextStyle(fontSize: 45, fontWeight: FontWeight.w400),
displaySmall: TextStyle(fontSize: 36, fontWeight: FontWeight.w400),
// Titeltext
headlineLarge: TextStyle(fontSize: 32, fontWeight: FontWeight.w400),
headlineMedium: TextStyle(fontSize: 28, fontWeight: FontWeight.w400),
headlineSmall: TextStyle(fontSize: 24, fontWeight: FontWeight.w400),
// Titeltext
titleLarge: TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
titleMedium: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
titleSmall: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
// Fließtext
bodyLarge: TextStyle(fontSize: 16, fontWeight: FontWeight.w400),
bodyMedium: TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
bodySmall: TextStyle(fontSize: 12, fontWeight: FontWeight.w400),
// Label-Text
labelLarge: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
labelMedium: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
labelSmall: TextStyle(fontSize: 11, fontWeight: FontWeight.w500),
),
)
textTheme: const TextTheme(
// Anzeigetext (Überschrift)
displayLarge: TextStyle(fontSize: 57, fontWeight: FontWeight.w400),
displayMedium: TextStyle(fontSize: 45, fontWeight: FontWeight.w400),
displaySmall: TextStyle(fontSize: 36, fontWeight: FontWeight.w400),
// Titeltext
headlineLarge: TextStyle(fontSize: 32, fontWeight: FontWeight.w400),
headlineMedium: TextStyle(fontSize: 28, fontWeight: FontWeight.w400),
headlineSmall: TextStyle(fontSize: 24, fontWeight: FontWeight.w400),
// Titeltext
titleLarge: TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
titleMedium: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
titleSmall: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
// Fließtext
bodyLarge: TextStyle(fontSize: 16, fontWeight: FontWeight.w400),
bodyMedium: TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
bodySmall: TextStyle(fontSize: 12, fontWeight: FontWeight.w400),
// Label-Text
labelLarge: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
labelMedium: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
labelSmall: TextStyle(fontSize: 11, fontWeight: FontWeight.w500),
),
)
Weitere ErweiterungenDie zentrale Verwaltung von App-Stilen mit ThemeData erleichtert den Wechsel von Themes und die Beibehaltung der Stilkonsistenz, was die Wartung vereinfacht.