ThemeData constructor Null safety

ThemeData(
  1. {Brightness? brightness,
  2. VisualDensity? visualDensity,
  3. MaterialColor? primarySwatch,
  4. Color? primaryColor,
  5. Brightness? primaryColorBrightness,
  6. Color? primaryColorLight,
  7. Color? primaryColorDark,
  8. @Deprecated('Use colorScheme.secondary instead. ' 'For more information, consult the migration guide at ' 'https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. ' 'This feature was deprecated after v2.3.0-0.1.pre.') Color? accentColor,
  9. @Deprecated('No longer used by the framework, please remove any reference to it. ' 'For more information, consult the migration guide at ' 'https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. ' 'This feature was deprecated after v2.3.0-0.1.pre.') Brightness? accentColorBrightness,
  10. Color? canvasColor,
  11. Color? shadowColor,
  12. Color? scaffoldBackgroundColor,
  13. Color? bottomAppBarColor,
  14. Color? cardColor,
  15. Color? dividerColor,
  16. Color? focusColor,
  17. Color? hoverColor,
  18. Color? highlightColor,
  19. Color? splashColor,
  20. InteractiveInkFeatureFactory? splashFactory,
  21. Color? selectedRowColor,
  22. Color? unselectedWidgetColor,
  23. Color? disabledColor,
  24. @Deprecated('No longer used by the framework, please remove any reference to it. ' 'This feature was deprecated after v2.3.0-0.2.pre.') Color? buttonColor,
  25. ButtonThemeData? buttonTheme,
  26. ToggleButtonsThemeData? toggleButtonsTheme,
  27. Color? secondaryHeaderColor,
  28. @Deprecated('Use TextSelectionThemeData.selectionColor instead. ' 'This feature was deprecated after v1.26.0-18.0.pre.') Color? textSelectionColor,
  29. @Deprecated('Use TextSelectionThemeData.cursorColor instead. ' 'This feature was deprecated after v1.26.0-18.0.pre.') Color? cursorColor,
  30. @Deprecated('Use TextSelectionThemeData.selectionHandleColor instead. ' 'This feature was deprecated after v1.26.0-18.0.pre.') Color? textSelectionHandleColor,
  31. Color? backgroundColor,
  32. Color? dialogBackgroundColor,
  33. Color? indicatorColor,
  34. Color? hintColor,
  35. Color? errorColor,
  36. Color? toggleableActiveColor,
  37. String? fontFamily,
  38. TextTheme? textTheme,
  39. TextTheme? primaryTextTheme,
  40. @Deprecated('No longer used by the framework, please remove any reference to it. ' 'For more information, consult the migration guide at ' 'https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. ' 'This feature was deprecated after v2.3.0-0.1.pre.') TextTheme? accentTextTheme,
  41. InputDecorationTheme? inputDecorationTheme,
  42. IconThemeData? iconTheme,
  43. IconThemeData? primaryIconTheme,
  44. @Deprecated('No longer used by the framework, please remove any reference to it. ' 'For more information, consult the migration guide at ' 'https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. ' 'This feature was deprecated after v2.3.0-0.1.pre.') IconThemeData? accentIconTheme,
  45. SliderThemeData? sliderTheme,
  46. TabBarTheme? tabBarTheme,
  47. TooltipThemeData? tooltipTheme,
  48. CardTheme? cardTheme,
  49. ChipThemeData? chipTheme,
  50. TargetPlatform? platform,
  51. MaterialTapTargetSize? materialTapTargetSize,
  52. bool? applyElevationOverlayColor,
  53. PageTransitionsTheme? pageTransitionsTheme,
  54. AppBarTheme? appBarTheme,
  55. ScrollbarThemeData? scrollbarTheme,
  56. BottomAppBarTheme? bottomAppBarTheme,
  57. ColorScheme? colorScheme,
  58. DialogTheme? dialogTheme,
  59. FloatingActionButtonThemeData? floatingActionButtonTheme,
  60. NavigationRailThemeData? navigationRailTheme,
  61. Typography? typography,
  62. NoDefaultCupertinoThemeData? cupertinoOverrideTheme,
  63. SnackBarThemeData? snackBarTheme,
  64. BottomSheetThemeData? bottomSheetTheme,
  65. PopupMenuThemeData? popupMenuTheme,
  66. MaterialBannerThemeData? bannerTheme,
  67. DividerThemeData? dividerTheme,
  68. ButtonBarThemeData? buttonBarTheme,
  69. BottomNavigationBarThemeData? bottomNavigationBarTheme,
  70. TimePickerThemeData? timePickerTheme,
  71. TextButtonThemeData? textButtonTheme,
  72. ElevatedButtonThemeData? elevatedButtonTheme,
  73. OutlinedButtonThemeData? outlinedButtonTheme,
  74. TextSelectionThemeData? textSelectionTheme,
  75. DataTableThemeData? dataTableTheme,
  76. CheckboxThemeData? checkboxTheme,
  77. RadioThemeData? radioTheme,
  78. SwitchThemeData? switchTheme,
  79. ProgressIndicatorThemeData? progressIndicatorTheme,
  80. @Deprecated('This "fix" is now enabled by default. ' 'This feature was deprecated after v2.5.0-1.0.pre.') bool? fixTextFieldOutlineLabel,
  81. @Deprecated('No longer used by the framework, please remove any reference to it. ' 'This feature was deprecated after v1.23.0-4.0.pre.') bool? useTextSelectionTheme}
)

Create a ThemeData that's used to configure a Theme.

Typically, only the brightness, primaryColor, or primarySwatch are specified. That pair of values are used to construct the colorScheme.

The colorScheme and textTheme are used by the Material components to compute default values for visual properties. The API documentation for each component widget explains exactly how the defaults are computed.

The textTheme TextStyle colors are black if the color scheme's brightness is Brightness.light, and white for Brightness.dark.

To override the appearance of specific components, provide a component theme parameter like sliderTheme, toggleButtonsTheme, or bottomNavigationBarTheme.

See also:

Implementation

factory ThemeData({
  Brightness? brightness,
  VisualDensity? visualDensity,
  MaterialColor? primarySwatch,
  Color? primaryColor,
  Brightness? primaryColorBrightness,
  Color? primaryColorLight,
  Color? primaryColorDark,
  @Deprecated(
    'Use colorScheme.secondary instead. '
    'For more information, consult the migration guide at '
    'https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. '
    'This feature was deprecated after v2.3.0-0.1.pre.',
  )
  Color? accentColor,
  @Deprecated(
    'No longer used by the framework, please remove any reference to it. '
    'For more information, consult the migration guide at '
    'https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. '
    'This feature was deprecated after v2.3.0-0.1.pre.',
  )
  Brightness? accentColorBrightness,
  Color? canvasColor,
  Color? shadowColor,
  Color? scaffoldBackgroundColor,
  Color? bottomAppBarColor,
  Color? cardColor,
  Color? dividerColor,
  Color? focusColor,
  Color? hoverColor,
  Color? highlightColor,
  Color? splashColor,
  InteractiveInkFeatureFactory? splashFactory,
  Color? selectedRowColor,
  Color? unselectedWidgetColor,
  Color? disabledColor,
  @Deprecated(
    'No longer used by the framework, please remove any reference to it. '
    'This feature was deprecated after v2.3.0-0.2.pre.',
  )
  Color? buttonColor,
  ButtonThemeData? buttonTheme,
  ToggleButtonsThemeData? toggleButtonsTheme,
  Color? secondaryHeaderColor,
  @Deprecated(
    'Use TextSelectionThemeData.selectionColor instead. '
    'This feature was deprecated after v1.26.0-18.0.pre.',
  )
  Color? textSelectionColor,
  @Deprecated(
    'Use TextSelectionThemeData.cursorColor instead. '
    'This feature was deprecated after v1.26.0-18.0.pre.',
  )
  Color? cursorColor,
  @Deprecated(
    'Use TextSelectionThemeData.selectionHandleColor instead. '
    'This feature was deprecated after v1.26.0-18.0.pre.',
  )
  Color? textSelectionHandleColor,
  Color? backgroundColor,
  Color? dialogBackgroundColor,
  Color? indicatorColor,
  Color? hintColor,
  Color? errorColor,
  Color? toggleableActiveColor,
  String? fontFamily,
  TextTheme? textTheme,
  TextTheme? primaryTextTheme,
  @Deprecated(
    'No longer used by the framework, please remove any reference to it. '
    'For more information, consult the migration guide at '
    'https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. '
    'This feature was deprecated after v2.3.0-0.1.pre.',
  )
  TextTheme? accentTextTheme,
  InputDecorationTheme? inputDecorationTheme,
  IconThemeData? iconTheme,
  IconThemeData? primaryIconTheme,
  @Deprecated(
    'No longer used by the framework, please remove any reference to it. '
    'For more information, consult the migration guide at '
    'https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. '
    'This feature was deprecated after v2.3.0-0.1.pre.',
  )
  IconThemeData? accentIconTheme,
  SliderThemeData? sliderTheme,
  TabBarTheme? tabBarTheme,
  TooltipThemeData? tooltipTheme,
  CardTheme? cardTheme,
  ChipThemeData? chipTheme,
  TargetPlatform? platform,
  MaterialTapTargetSize? materialTapTargetSize,
  bool? applyElevationOverlayColor,
  PageTransitionsTheme? pageTransitionsTheme,
  AppBarTheme? appBarTheme,
  ScrollbarThemeData? scrollbarTheme,
  BottomAppBarTheme? bottomAppBarTheme,
  ColorScheme? colorScheme,
  DialogTheme? dialogTheme,
  FloatingActionButtonThemeData? floatingActionButtonTheme,
  NavigationRailThemeData? navigationRailTheme,
  Typography? typography,
  NoDefaultCupertinoThemeData? cupertinoOverrideTheme,
  SnackBarThemeData? snackBarTheme,
  BottomSheetThemeData? bottomSheetTheme,
  PopupMenuThemeData? popupMenuTheme,
  MaterialBannerThemeData? bannerTheme,
  DividerThemeData? dividerTheme,
  ButtonBarThemeData? buttonBarTheme,
  BottomNavigationBarThemeData? bottomNavigationBarTheme,
  TimePickerThemeData? timePickerTheme,
  TextButtonThemeData? textButtonTheme,
  ElevatedButtonThemeData? elevatedButtonTheme,
  OutlinedButtonThemeData? outlinedButtonTheme,
  TextSelectionThemeData? textSelectionTheme,
  DataTableThemeData? dataTableTheme,
  CheckboxThemeData? checkboxTheme,
  RadioThemeData? radioTheme,
  SwitchThemeData? switchTheme,
  ProgressIndicatorThemeData? progressIndicatorTheme,
  @Deprecated(
    'This "fix" is now enabled by default. '
    'This feature was deprecated after v2.5.0-1.0.pre.',
  )
  bool? fixTextFieldOutlineLabel,
  @Deprecated(
    'No longer used by the framework, please remove any reference to it. '
    'This feature was deprecated after v1.23.0-4.0.pre.',
  )
  bool? useTextSelectionTheme,
}) {
  assert(colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness);
  final Brightness _brightness = brightness ?? colorScheme?.brightness ?? Brightness.light;
  final bool isDark = _brightness == Brightness.dark;
  visualDensity ??= VisualDensity.adaptivePlatformDensity;
  primarySwatch ??= Colors.blue;
  primaryColor ??= isDark ? Colors.grey[900]! : primarySwatch;
  primaryColorBrightness ??= estimateBrightnessForColor(primaryColor);
  primaryColorLight ??= isDark ? Colors.grey[500]! : primarySwatch[100]!;
  primaryColorDark ??= isDark ? Colors.black : primarySwatch[700]!;
  final bool primaryIsDark = primaryColorBrightness == Brightness.dark;
  toggleableActiveColor ??= isDark ? Colors.tealAccent[200]! : (accentColor ?? primarySwatch[600]!);
  accentColor ??= isDark ? Colors.tealAccent[200]! : primarySwatch[500]!;
  accentColorBrightness ??= estimateBrightnessForColor(accentColor);
  final bool accentIsDark = accentColorBrightness == Brightness.dark;
  canvasColor ??= isDark ? Colors.grey[850]! : Colors.grey[50]!;
  shadowColor ??= Colors.black;
  scaffoldBackgroundColor ??= canvasColor;
  bottomAppBarColor ??= isDark ? Colors.grey[800]! : Colors.white;
  cardColor ??= isDark ? Colors.grey[800]! : Colors.white;
  dividerColor ??= isDark ? const Color(0x1FFFFFFF) : const Color(0x1F000000);

  // Create a ColorScheme that is backwards compatible as possible
  // with the existing default ThemeData color values.
  colorScheme ??= ColorScheme.fromSwatch(
    primarySwatch: primarySwatch,
    primaryColorDark: primaryColorDark,
    accentColor: accentColor,
    cardColor: cardColor,
    backgroundColor: backgroundColor,
    errorColor: errorColor,
    brightness: _brightness,
  );

  splashFactory ??= InkSplash.splashFactory;
  selectedRowColor ??= Colors.grey[100]!;
  unselectedWidgetColor ??= isDark ? Colors.white70 : Colors.black54;
  // Spec doesn't specify a dark theme secondaryHeaderColor, this is a guess.
  secondaryHeaderColor ??= isDark ? Colors.grey[700]! : primarySwatch[50]!;
  textSelectionColor ??= isDark ? accentColor : primarySwatch[200]!;
  cursorColor = cursorColor ?? const Color.fromRGBO(66, 133, 244, 1.0);
  textSelectionHandleColor ??= isDark ? Colors.tealAccent[400]! : primarySwatch[300]!;
  backgroundColor ??= isDark ? Colors.grey[700]! : primarySwatch[200]!;
  dialogBackgroundColor ??= isDark ? Colors.grey[800]! : Colors.white;
  indicatorColor ??= accentColor == primaryColor ? Colors.white : accentColor;
  hintColor ??= isDark ? Colors.white60 : Colors.black.withOpacity(0.6);
  errorColor ??= Colors.red[700]!;
  inputDecorationTheme ??= const InputDecorationTheme();
  pageTransitionsTheme ??= const PageTransitionsTheme();
  primaryIconTheme ??= primaryIsDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black);
  accentIconTheme ??= accentIsDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black);
  iconTheme ??= isDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black87);
  platform ??= defaultTargetPlatform;
  typography ??= Typography.material2014(platform: platform);
  TextTheme defaultTextTheme = isDark ? typography.white : typography.black;
  TextTheme defaultPrimaryTextTheme = primaryIsDark ? typography.white : typography.black;
  TextTheme defaultAccentTextTheme = accentIsDark ? typography.white : typography.black;
  if (fontFamily != null) {
    defaultTextTheme = defaultTextTheme.apply(fontFamily: fontFamily);
    defaultPrimaryTextTheme = defaultPrimaryTextTheme.apply(fontFamily: fontFamily);
    defaultAccentTextTheme = defaultAccentTextTheme.apply(fontFamily: fontFamily);
  }
  textTheme = defaultTextTheme.merge(textTheme);
  primaryTextTheme = defaultPrimaryTextTheme.merge(primaryTextTheme);
  accentTextTheme = defaultAccentTextTheme.merge(accentTextTheme);
  switch (platform) {
    case TargetPlatform.android:
    case TargetPlatform.fuchsia:
    case TargetPlatform.iOS:
      materialTapTargetSize ??= MaterialTapTargetSize.padded;
      break;
    case TargetPlatform.linux:
    case TargetPlatform.macOS:
    case TargetPlatform.windows:
       materialTapTargetSize ??= MaterialTapTargetSize.shrinkWrap;
      break;
  }
  applyElevationOverlayColor ??= false;

  // Used as the default color (fill color) for RaisedButtons. Computing the
  // default for ButtonThemeData for the sake of backwards compatibility.
  buttonColor ??= isDark ? primarySwatch[600]! : Colors.grey[300]!;
  focusColor ??= isDark ? Colors.white.withOpacity(0.12) : Colors.black.withOpacity(0.12);
  hoverColor ??= isDark ? Colors.white.withOpacity(0.04) : Colors.black.withOpacity(0.04);
  buttonTheme ??= ButtonThemeData(
    colorScheme: colorScheme,
    buttonColor: buttonColor,
    disabledColor: disabledColor,
    focusColor: focusColor,
    hoverColor: hoverColor,
    highlightColor: highlightColor,
    splashColor: splashColor,
    materialTapTargetSize: materialTapTargetSize,
  );
  toggleButtonsTheme ??= const ToggleButtonsThemeData();
  disabledColor ??= isDark ? Colors.white38 : Colors.black38;
  highlightColor ??= isDark ? _kDarkThemeHighlightColor : _kLightThemeHighlightColor;
  splashColor ??= isDark ? _kDarkThemeSplashColor : _kLightThemeSplashColor;

  sliderTheme ??= const SliderThemeData();
  tabBarTheme ??= const TabBarTheme();
  tooltipTheme ??= const TooltipThemeData();
  appBarTheme ??= const AppBarTheme();
  scrollbarTheme ??= const ScrollbarThemeData();
  bottomAppBarTheme ??= const BottomAppBarTheme();
  cardTheme ??= const CardTheme();
  chipTheme ??= ChipThemeData.fromDefaults(
    secondaryColor: isDark ? Colors.tealAccent[200]! : primaryColor,
    brightness: colorScheme.brightness,
    labelStyle: textTheme.bodyText1!,
  );
  dialogTheme ??= const DialogTheme();
  floatingActionButtonTheme ??= const FloatingActionButtonThemeData();
  navigationRailTheme ??= const NavigationRailThemeData();
  cupertinoOverrideTheme = cupertinoOverrideTheme?.noDefault();
  snackBarTheme ??= const SnackBarThemeData();
  bottomSheetTheme ??= const BottomSheetThemeData();
  popupMenuTheme ??= const PopupMenuThemeData();
  bannerTheme ??= const MaterialBannerThemeData();
  dividerTheme ??= const DividerThemeData();
  buttonBarTheme ??= const ButtonBarThemeData();
  bottomNavigationBarTheme ??= const BottomNavigationBarThemeData();
  timePickerTheme ??= const TimePickerThemeData();
  textButtonTheme ??= const TextButtonThemeData();
  elevatedButtonTheme ??= const ElevatedButtonThemeData();
  outlinedButtonTheme ??= const OutlinedButtonThemeData();
  textSelectionTheme ??= const TextSelectionThemeData();
  dataTableTheme ??= const DataTableThemeData();
  checkboxTheme ??= const CheckboxThemeData();
  radioTheme ??= const RadioThemeData();
  switchTheme ??= const SwitchThemeData();
  progressIndicatorTheme ??= const ProgressIndicatorThemeData();

  fixTextFieldOutlineLabel ??= true;
  useTextSelectionTheme ??= true;

  return ThemeData.raw(
    visualDensity: visualDensity,
    primaryColor: primaryColor,
    primaryColorBrightness: primaryColorBrightness,
    primaryColorLight: primaryColorLight,
    primaryColorDark: primaryColorDark,
    accentColor: accentColor,
    accentColorBrightness: accentColorBrightness,
    canvasColor: canvasColor,
    shadowColor: shadowColor,
    scaffoldBackgroundColor: scaffoldBackgroundColor,
    bottomAppBarColor: bottomAppBarColor,
    cardColor: cardColor,
    dividerColor: dividerColor,
    focusColor: focusColor,
    hoverColor: hoverColor,
    highlightColor: highlightColor,
    splashColor: splashColor,
    splashFactory: splashFactory,
    selectedRowColor: selectedRowColor,
    unselectedWidgetColor: unselectedWidgetColor,
    disabledColor: disabledColor,
    buttonTheme: buttonTheme,
    buttonColor: buttonColor,
    toggleButtonsTheme: toggleButtonsTheme,
    toggleableActiveColor: toggleableActiveColor,
    secondaryHeaderColor: secondaryHeaderColor,
    textSelectionColor: textSelectionColor,
    cursorColor: cursorColor,
    textSelectionHandleColor: textSelectionHandleColor,
    backgroundColor: backgroundColor,
    dialogBackgroundColor: dialogBackgroundColor,
    indicatorColor: indicatorColor,
    hintColor: hintColor,
    errorColor: errorColor,
    textTheme: textTheme,
    primaryTextTheme: primaryTextTheme,
    accentTextTheme: accentTextTheme,
    inputDecorationTheme: inputDecorationTheme,
    iconTheme: iconTheme,
    primaryIconTheme: primaryIconTheme,
    accentIconTheme: accentIconTheme,
    sliderTheme: sliderTheme,
    tabBarTheme: tabBarTheme,
    tooltipTheme: tooltipTheme,
    cardTheme: cardTheme,
    chipTheme: chipTheme,
    platform: platform,
    materialTapTargetSize: materialTapTargetSize,
    applyElevationOverlayColor: applyElevationOverlayColor,
    pageTransitionsTheme: pageTransitionsTheme,
    appBarTheme: appBarTheme,
    scrollbarTheme: scrollbarTheme,
    bottomAppBarTheme: bottomAppBarTheme,
    colorScheme: colorScheme,
    dialogTheme: dialogTheme,
    floatingActionButtonTheme: floatingActionButtonTheme,
    navigationRailTheme: navigationRailTheme,
    typography: typography,
    cupertinoOverrideTheme: cupertinoOverrideTheme,
    snackBarTheme: snackBarTheme,
    bottomSheetTheme: bottomSheetTheme,
    popupMenuTheme: popupMenuTheme,
    bannerTheme: bannerTheme,
    dividerTheme: dividerTheme,
    buttonBarTheme: buttonBarTheme,
    bottomNavigationBarTheme: bottomNavigationBarTheme,
    timePickerTheme: timePickerTheme,
    textButtonTheme: textButtonTheme,
    elevatedButtonTheme: elevatedButtonTheme,
    outlinedButtonTheme: outlinedButtonTheme,
    textSelectionTheme: textSelectionTheme,
    dataTableTheme: dataTableTheme,
    checkboxTheme: checkboxTheme,
    radioTheme: radioTheme,
    switchTheme: switchTheme,
    progressIndicatorTheme: progressIndicatorTheme,
    fixTextFieldOutlineLabel: fixTextFieldOutlineLabel,
    useTextSelectionTheme: useTextSelectionTheme,
  );
}