menuTheme static method

MenuThemeData menuTheme({
  1. required ColorScheme colorScheme,
  2. SchemeColor? backgroundSchemeColor,
  3. double? opacity,
  4. double? radius,
  5. EdgeInsetsGeometry? padding,
  6. double? elevation,
  7. Color? surfaceTintColor,
})

An opinionated MenuThemeData theme.

This theme is used by the menu for the DropDownMenu, MenuBar and MenuAnchor.

Implementation

static MenuThemeData menuTheme({
  /// Typically the same [ColorScheme] that is also used for your [ThemeData].
  required final ColorScheme colorScheme,

  /// Defines which [Theme] based [ColorScheme] based background color
  /// of [PopupMenuButton].
  ///
  /// If not defined, will remains null and via Flutter SDK defaults get
  /// [ColorScheme.surface] color.
  final SchemeColor? backgroundSchemeColor,

  /// Menu background opacity.
  ///
  /// Used by FlexColorScheme to modify the opacity the themed [MenuBar],
  /// [MenuAnchor] and [DropDownMenu] background color.
  ///
  /// Defaults to undefined (null).
  /// If undefined, produced result is same as 1, fully opaque.
  ///
  /// If opacity is defined and [backgroundSchemeColor] is undefined,
  /// then [ColorScheme.surface] will be used as background color to
  /// make a background color with opacity.
  final double? opacity,

  /// Menu corner radius.
  ///
  /// If not defined, default to 4 via Menu widget Flutter SDK defaults.
  final double? radius,

  /// The padding between the menu's boundary and its child.
  final EdgeInsetsGeometry? padding,

  /// Popup menu elevation.
  ///
  /// If not defined, defaults to 3 dp via Flutter widget SDK defaults.
  final double? elevation,

  /// Overrides the default value for MenuThemeData
  /// [menuStyle.surfaceTintColor].
  final Color? surfaceTintColor,
}) {
  // Get effective background color.
  final Color? backgroundColor = backgroundSchemeColor != null
      ? schemeColor(backgroundSchemeColor, colorScheme)
          .withOpacity(opacity ?? 1.0)
      : opacity != null
          ? colorScheme.surface.withOpacity(opacity)
          : null;

  final bool allDefaults = backgroundSchemeColor == null &&
      opacity == null &&
      radius == null &&
      padding == null &&
      elevation == null &&
      surfaceTintColor == null;

  return MenuThemeData(
    style: allDefaults
        ? null
        : MenuStyle(
            elevation: elevation == null
                ? null
                : MaterialStatePropertyAll<double?>(elevation),
            backgroundColor: backgroundSchemeColor == null && opacity == null
                ? null
                : MaterialStatePropertyAll<Color?>(backgroundColor),
            padding: padding == null
                ? null
                : MaterialStatePropertyAll<EdgeInsetsGeometry?>(padding),
            surfaceTintColor: surfaceTintColor == null
                ? null
                : MaterialStatePropertyAll<Color>(surfaceTintColor),
            shape: radius == null
                ? null
                : MaterialStatePropertyAll<OutlinedBorder>(
                    RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(
                        Radius.circular(radius),
                      ),
                    ),
                  ),
          ),
  );
}