Using themes
Learning objectives
- You know how to set a theme for an application.
So far, we've created applications that use the default theme provided by Flutter. The default theme includes the blue color in the application bar, in buttons, and so on, as well as specific font sizes and background colors. A theme of an application is defined by passing a ThemeData object to the theme
parameter of the MaterialApp
constructor. The following example demonstrates the use of ThemeData.light, which is the default theme of Flutter.
Flutter provides also a default dark mode theme, available through ThemeData.dark. If we would wish that our application used the dark mode, we would change the ThemeData
object that we pass to MaterialApp
as follows.
Devices often have a specific configuration for dark mode that can be enabled either by the user, or by different automations such as rules based on hour of day. The automatic changing to (and out of) dark mode has also started to emerge in other devices such as car dashboards, where dark mode be enabled e.g. based on light sensors of the car. This allows automatically enabling and disabling the dark mode during e.g. night time and when entering and leaving a tunnel.
Flutter also provides the possibility to define both a default theme and a dark theme to the application. To define both, we provide themes to the theme
and darkTheme
properties of MaterialApp
.