styleFrom static method

ButtonStyle styleFrom({
  1. Color? foregroundColor,
  2. Color? backgroundColor,
  3. Color? selectedForegroundColor,
  4. Color? selectedBackgroundColor,
  5. Color? disabledForegroundColor,
  6. Color? disabledBackgroundColor,
  7. Color? shadowColor,
  8. Color? surfaceTintColor,
  9. Color? iconColor,
  10. double? iconSize,
  11. Color? disabledIconColor,
  12. Color? overlayColor,
  13. double? elevation,
  14. TextStyle? textStyle,
  15. EdgeInsetsGeometry? padding,
  16. Size? minimumSize,
  17. Size? fixedSize,
  18. Size? maximumSize,
  19. BorderSide? side,
  20. OutlinedBorder? shape,
  21. MouseCursor? enabledMouseCursor,
  22. MouseCursor? disabledMouseCursor,
  23. VisualDensity? visualDensity,
  24. MaterialTapTargetSize? tapTargetSize,
  25. Duration? animationDuration,
  26. bool? enableFeedback,
  27. AlignmentGeometry? alignment,
  28. InteractiveInkFeatureFactory? splashFactory,
})

A static convenience method that constructs a segmented button ButtonStyle given simple values.

The foregroundColor, selectedForegroundColor, and disabledForegroundColor colors are used to create a WidgetStateProperty ButtonStyle.foregroundColor, and a derived ButtonStyle.overlayColor if overlayColor isn't specified.

If overlayColor is specified and its value is Colors.transparent then the pressed/focused/hovered highlights are effectively defeated. Otherwise a WidgetStateProperty with the same opacities as the default is created.

The backgroundColor, selectedBackgroundColor and disabledBackgroundColor colors are used to create a WidgetStateProperty ButtonStyle.backgroundColor.

Similarly, the enabledMouseCursor and disabledMouseCursor parameters are used to construct ButtonStyle.mouseCursor.

The iconColor, disabledIconColor are used to construct ButtonStyle.iconColor and iconSize is used to construct ButtonStyle.iconSize.

All of the other parameters are either used directly or used to create a WidgetStateProperty with a single value for all states.

All parameters default to null. By default this method returns a ButtonStyle that doesn't override anything.

For example, to override the default text and icon colors for a SegmentedButton, as well as its overlay color, with all of the standard opacity adjustments for the pressed, focused, and hovered states, one could write:
link
SegmentedButton<int>(
  style: SegmentedButton.styleFrom(
    foregroundColor: Colors.black,
    selectedForegroundColor: Colors.white,
    backgroundColor: Colors.amber,
    selectedBackgroundColor: Colors.red,
  ),
  segments: const <ButtonSegment<int>>[
    ButtonSegment<int>(
      value: 0,
      label: Text('0'),
      icon: Icon(Icons.calendar_view_day),
    ),
    ButtonSegment<int>(
      value: 1,
      label: Text('1'),
      icon: Icon(Icons.calendar_view_week),
    ),
  ],
  selected: const <int>{0},
  onSelectionChanged: (Set<int> selection) {},
),

Implementation

static ButtonStyle styleFrom({
  Color? foregroundColor,
  Color? backgroundColor,
  Color? selectedForegroundColor,
  Color? selectedBackgroundColor,
  Color? disabledForegroundColor,
  Color? disabledBackgroundColor,
  Color? shadowColor,
  Color? surfaceTintColor,
  Color? iconColor,
  double? iconSize,
  Color? disabledIconColor,
  Color? overlayColor,
  double? elevation,
  TextStyle? textStyle,
  EdgeInsetsGeometry? padding,
  Size? minimumSize,
  Size? fixedSize,
  Size? maximumSize,
  BorderSide? side,
  OutlinedBorder? shape,
  MouseCursor? enabledMouseCursor,
  MouseCursor? disabledMouseCursor,
  VisualDensity? visualDensity,
  MaterialTapTargetSize? tapTargetSize,
  Duration? animationDuration,
  bool? enableFeedback,
  AlignmentGeometry? alignment,
  InteractiveInkFeatureFactory? splashFactory,
}) {
  final MaterialStateProperty<Color?>? overlayColorProp = (foregroundColor == null &&
    selectedForegroundColor == null && overlayColor == null)
      ? null
      : switch (overlayColor) {
          (final Color overlayColor) when overlayColor.value == 0 => const MaterialStatePropertyAll<Color?>(Colors.transparent),
          _ => _SegmentedButtonDefaultsM3.resolveStateColor(foregroundColor, selectedForegroundColor, overlayColor),
        };
  return TextButton.styleFrom(
    textStyle: textStyle,
    shadowColor: shadowColor,
    surfaceTintColor: surfaceTintColor,
    iconColor: iconColor,
    iconSize: iconSize,
    disabledIconColor: disabledIconColor,
    elevation: elevation,
    padding: padding,
    minimumSize: minimumSize,
    fixedSize: fixedSize,
    maximumSize: maximumSize,
    side: side,
    shape: shape,
    enabledMouseCursor: enabledMouseCursor,
    disabledMouseCursor: disabledMouseCursor,
    visualDensity: visualDensity,
    tapTargetSize: tapTargetSize,
    animationDuration: animationDuration,
    enableFeedback: enableFeedback,
    alignment: alignment,
    splashFactory: splashFactory,
  ).copyWith(
    foregroundColor: _defaultColor(foregroundColor, disabledForegroundColor, selectedForegroundColor),
    backgroundColor: _defaultColor(backgroundColor, disabledBackgroundColor, selectedBackgroundColor),
    overlayColor: overlayColorProp,
  );
}