getTextColor method Null safety

Color getTextColor(
  1. MaterialButton button
)

The foreground color of the button's text and icon.

If button is not MaterialButton.enabled, the value of getDisabledTextColor is returned. If the button is enabled and MaterialButton.textColor is non-null, then MaterialButton.textColor is returned.

Otherwise the text color depends on the value of getTextTheme and getBrightness.

Implementation

Color getTextColor(MaterialButton button) {
  if (!button.enabled)
    return getDisabledTextColor(button);

  if (button.textColor != null)
    return button.textColor!;

  switch (getTextTheme(button)) {
    case ButtonTextTheme.normal:
      return getBrightness(button) == Brightness.dark ? Colors.white : Colors.black87;

    case ButtonTextTheme.accent:
      return colorScheme!.secondary;

    case ButtonTextTheme.primary:
      final Color? fillColor = getFillColor(button);
      final bool fillIsDark = fillColor != null
        ? ThemeData.estimateBrightnessForColor(fillColor) == Brightness.dark
        : getBrightness(button) == Brightness.dark;
      if (fillIsDark)
        return Colors.white;
      if (button is FlatButton)
        return colorScheme!.primary;
      return Colors.black;
  }
}