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? overlayColor,
  10. double? elevation,
  11. TextStyle? textStyle,
  12. EdgeInsetsGeometry? padding,
  13. Size? minimumSize,
  14. Size? fixedSize,
  15. Size? maximumSize,
  16. BorderSide? side,
  17. OutlinedBorder? shape,
  18. MouseCursor? enabledMouseCursor,
  19. MouseCursor? disabledMouseCursor,
  20. VisualDensity? visualDensity,
  21. MaterialTapTargetSize? tapTargetSize,
  22. Duration? animationDuration,
  23. bool? enableFeedback,
  24. AlignmentGeometry? alignment,
  25. 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 MaterialStateProperty 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 MaterialStateProperty with the same opacities as the default is created.

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

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

All of the other parameters are either used directly or used to create a MaterialStateProperty 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? 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?>? foregroundColorProp =
    (foregroundColor == null && disabledForegroundColor == null && selectedForegroundColor == null)
      ? null
      : _SegmentButtonDefaultColor(foregroundColor, disabledForegroundColor, selectedForegroundColor);
  final MaterialStateProperty<Color?>? backgroundColorProp =
    (backgroundColor == null && disabledBackgroundColor == null && selectedBackgroundColor == null)
      ? null
      : _SegmentButtonDefaultColor(backgroundColor, disabledBackgroundColor, selectedBackgroundColor);
  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,
    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: foregroundColorProp,
    backgroundColor: backgroundColorProp,
    overlayColor: overlayColorProp,
  );
}