Defining themes
Learning objectives
- You know how to define themes.
Themes are defined by creating a new ThemeData object and passing appropriate styles to it as properties. The properties of ThemeData are outlined in the documentation of the class. There are plenty of things to adjust. For example, if we would wish to change the color scheme of the application, we would provide a value to the colorScheme
property, while if we would wish to change e.g. text theme, we would provide a new value to the textTheme
property.
The values are often property-specific. As an example, the colorScheme
is given an instance of ColorScheme, which is used to define -- well -- the color scheme of the application. For convenience, the class provides a named constructor fromSwatch
with the named argument primarySwatch
(and others) that can be used to build a color scheme from an instance of MaterialColor. For example, the following would create an application that would use shades of pink as the primary colors.
Similarly, the following example outlines the use of TextTheme that is used to adjust the text related themes. The TextTheme class has a set of arguments that can be used to adjust how different types of text is shown. Each of these arguments take an instance of TextStyle as a parameter. In the following example, we provide a new style for titleMedium
, outlining that all titleMedium
texts use the brown color.
Fonts as project assets
Fonts can also be added as assets to the Flutter project. The Flutter cookbook on fonts provides a good summary of how this is done.