This page talks about few points I noted about Custom theming in a Flutter application.
#1 ColorScheme
ColorScheme
is a class that contains a set of twelve colors based on the Material spec that can be used to configure the color properties of most components.
#2 Out of the Box Color Schemes
Each of these built-in color schemes apply different colors to these 13 properties.
ColorScheme.light
ColorScheme.dark
ColorScheme.highContrastLight
ColorScheme.highContrastDark
How to use these Color Schemes
By supplying a Color scheme to the ThemeData.from()
factory constructor, entire app's theme can be configured.
A Color Scheme can also be created using the Primary Swatch color.
- The remaining properties of the
ColorScheme
are set in theColorScheme.fromSwatch
constructor.
#3 Changing only the required colors — Using copyWith
Example taken from Flutter source theme_data.dart
- The Color scheme is based on
Colors.blue
- The Color scheme’s secondary color is overridden with
Colors.green
- The Body Text 2 Color is changed to
Colors.purple
#4 How to create Custom themes
The theming example here is taken from Flokk. I just changed the state implementation from Provider to Riverpod.
custom_theme.dart
- The
enum
defines two themes - Light & Dark. - The
AppTheme
class defines properties to hold a variety of colors. It also has a factory constructorAppTheme.fromType()
. - To use Custom theme, we need a
ThemeData
object which will be passed toMaterialApp()
. So, theAppTheme
class also has a getterthemeData
that creates aThemeData
object by configuringtextTheme
andcolorScheme
. Few other properties ofThemeData
are also changed by usingcopyWith()
. - The
ColorScheme
class requires 13 properties to be passed. This is where the colors fromAppTheme
are used.
main.dart
StateNotifier
is designed to be subclassed. We first need to pass an initial value to thesuper
constructor, to define the initial state of our object.