ChipThemeData.fromDefaults constructor

ChipThemeData.fromDefaults({
  1. Brightness? brightness,
  2. Color? primaryColor,
  3. required Color secondaryColor,
  4. required TextStyle labelStyle,
})

Generates a ChipThemeData from a brightness, a primary color, and a text style.

The brightness is used to select a primary color from the default values.

The optional primaryColor is used as the base color for the other colors. The opacity of the primaryColor is ignored. If a primaryColor is specified, then the brightness is ignored, and the theme brightness is determined from the primaryColor.

Only one of primaryColor or brightness may be specified.

The secondaryColor is used for the selection colors needed by ChoiceChip.

This is used to generate the default chip theme for a ThemeData.

Implementation

factory ChipThemeData.fromDefaults({
  Brightness? brightness,
  Color? primaryColor,
  required Color secondaryColor,
  required TextStyle labelStyle,
}) {
  assert(primaryColor != null || brightness != null, 'One of primaryColor or brightness must be specified');
  assert(primaryColor == null || brightness == null, 'Only one of primaryColor or brightness may be specified');

  if (primaryColor != null) {
    brightness = ThemeData.estimateBrightnessForColor(primaryColor);
  }

  // These are Material Design defaults, and are used to derive
  // component Colors (with opacity) from base colors.
  const int backgroundAlpha = 0x1f; // 12%
  const int deleteIconAlpha = 0xde; // 87%
  const int disabledAlpha = 0x0c; // 38% * 12% = 5%
  const int selectAlpha = 0x3d; // 12% + 12% = 24%
  const int textLabelAlpha = 0xde; // 87%
  const EdgeInsetsGeometry padding = EdgeInsets.all(4.0);

  primaryColor = primaryColor ?? (brightness == Brightness.light ? Colors.black : Colors.white);
  final Color backgroundColor = primaryColor.withAlpha(backgroundAlpha);
  final Color deleteIconColor = primaryColor.withAlpha(deleteIconAlpha);
  final Color disabledColor = primaryColor.withAlpha(disabledAlpha);
  final Color selectedColor = primaryColor.withAlpha(selectAlpha);
  final Color secondarySelectedColor = secondaryColor.withAlpha(selectAlpha);
  final TextStyle secondaryLabelStyle = labelStyle.copyWith(
    color: secondaryColor.withAlpha(textLabelAlpha),
  );
  labelStyle = labelStyle.copyWith(color: primaryColor.withAlpha(textLabelAlpha));

  return ChipThemeData(
    backgroundColor: backgroundColor,
    deleteIconColor: deleteIconColor,
    disabledColor: disabledColor,
    selectedColor: selectedColor,
    secondarySelectedColor: secondarySelectedColor,
    shadowColor: Colors.black,
    selectedShadowColor: Colors.black,
    showCheckmark: true,
    padding: padding,
    labelStyle: labelStyle,
    secondaryLabelStyle: secondaryLabelStyle,
    brightness: brightness,
    elevation: 0.0,
    pressElevation: 8.0,
  );
}