createTheme static method
- required String name,
- required Color primaryColor,
- required Color secondaryColor,
- required Brightness brightness,
- Color? backgroundColor,
- Color? surfaceColor,
- Color? scaffoldBackgroundColor,
- Color? appBarColor,
- Color? cardColor,
- Color? dividerColor,
- Color? iconColor,
- Color? textColor,
- Color? hintColor,
- Color? disabledColor,
- Color? errorColor,
- Color? buttonColor,
- double? borderRadius,
- double? elevation,
- Color? fabColor,
- Color? inputFillColor,
- Color? inputBorderColor,
- String? fontFamily,
- FontWeight? fontWeight,
- TextStyle? headlineStyle,
- TextStyle? bodyStyle,
- TextStyle? buttonTextStyle,
- bool? useMaterial3,
- bool? useRippleEffect,
Creates a custom theme with the specified configuration.
Required: name, primaryColor, secondaryColor, brightness.
All other parameters are optional for detailed customization.
Throws Exception if name is 'light'/'dark' or already exists.
Implementation
static void createTheme({
required String name,
required Color primaryColor,
required Color secondaryColor,
required Brightness brightness,
Color? backgroundColor,
Color? surfaceColor,
Color? scaffoldBackgroundColor,
Color? appBarColor,
Color? cardColor,
Color? dividerColor,
Color? iconColor,
Color? textColor,
Color? hintColor,
Color? disabledColor,
Color? errorColor,
Color? buttonColor,
double? borderRadius,
double? elevation,
Color? fabColor,
Color? inputFillColor,
Color? inputBorderColor,
String? fontFamily,
FontWeight? fontWeight,
TextStyle? headlineStyle,
TextStyle? bodyStyle,
TextStyle? buttonTextStyle,
bool? useMaterial3,
bool? useRippleEffect,
}) {
if (name.toLowerCase() == 'light' || name.toLowerCase() == 'dark') {
throw Exception("Can't override default themes");
}
if (_instance._themes.containsKey(name)) {
throw Exception(
'Theme with name $name already exists, choose a different name',
);
}
final isLight = brightness == Brightness.light;
final theme = ThemeData(
useMaterial3: useMaterial3,
brightness: brightness,
primaryColor: primaryColor,
scaffoldBackgroundColor: scaffoldBackgroundColor,
cardColor: cardColor,
dividerColor: dividerColor,
disabledColor: disabledColor,
hintColor: hintColor,
iconTheme: IconThemeData(color: iconColor),
colorScheme: ColorScheme(
brightness: brightness,
primary: primaryColor,
onPrimary: textColor ?? Colors.white,
secondary: secondaryColor,
onSecondary: textColor ?? Colors.white,
error: errorColor ?? Colors.red,
onError: Colors.white,
surface:
surfaceColor ?? (isLight ? Colors.white : const Color(0xFF1E1E1E)),
onSurface: textColor ?? (isLight ? Colors.black : Colors.white),
),
appBarTheme: AppBarTheme(
backgroundColor: appBarColor,
foregroundColor: textColor,
elevation: elevation,
),
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: fabColor,
foregroundColor: textColor,
elevation: elevation,
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: buttonColor,
foregroundColor: textColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(borderRadius ?? 12),
),
elevation: elevation,
textStyle: buttonTextStyle,
),
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
foregroundColor: secondaryColor,
textStyle: TextStyle(fontWeight: fontWeight, fontFamily: fontFamily),
),
),
outlinedButtonTheme: OutlinedButtonThemeData(
style: OutlinedButton.styleFrom(
foregroundColor: secondaryColor,
side: BorderSide(color: secondaryColor),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(borderRadius ?? 12),
),
),
),
inputDecorationTheme: InputDecorationTheme(
filled: true,
fillColor: inputFillColor,
hintStyle: TextStyle(color: hintColor),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(borderRadius ?? 8),
borderSide: BorderSide(color: inputBorderColor ?? Colors.grey),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(borderRadius ?? 8),
borderSide: BorderSide(color: inputBorderColor ?? Colors.grey),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(borderRadius ?? 8),
borderSide: BorderSide(color: primaryColor, width: 2),
),
),
cardTheme: CardThemeData(
color: cardColor,
elevation: elevation,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(borderRadius ?? 12),
),
),
textTheme: TextTheme(headlineLarge: headlineStyle, bodyLarge: bodyStyle),
splashFactory: (useRippleEffect ?? true)
? InkRipple.splashFactory
: NoSplash.splashFactory,
);
_instance._themes[name] = theme;
}