createMasterButtonStyle function

ButtonStyle createMasterButtonStyle(
  1. ButtonNode node, {
  2. double? elevation,
  3. required ScopedValues scopedValues,
})

Flutter's ButtonStyle parameters do different things depending on the button type. So we have to manually change them.

Implementation

ButtonStyle createMasterButtonStyle(ButtonNode node,
    {double? elevation, required ScopedValues scopedValues}) {
  final TextStyle textStyle =
      TextUtils.retrieveTextStyleFromProp(node.properties.labelStyle);
  final Color? labelColor = PropertyValueDelegate.getPropertyValue<ColorRGBA>(
        node,
        'labelColor',
        scopedValues: scopedValues,
      )?.toFlutterColor() ??
      TextUtils.retrievePropColor(node.properties.labelStyle);

  final buttonColor = PropertyValueDelegate.getPropertyValue<ColorRGBA>(
        node,
        'buttonColor',
        scopedValues: scopedValues,
      ) ??
      node.properties.buttonColor;

  final shadowColor = PropertyValueDelegate.getPropertyValue<ColorRGBA>(
        node,
        'shadowColor',
        scopedValues: scopedValues,
      ) ??
      node.properties.shadowColor;

  ButtonStyle? buttonStyle;
  switch (node.properties.buttonType) {
    case ButtonTypeEnum.elevated:
      buttonStyle = ElevatedButton.styleFrom(
        foregroundColor: labelColor,
        elevation: elevation ?? node.properties.elevation,
        shadowColor: shadowColor.toFlutterColor(),
        backgroundColor: buttonColor.toFlutterColor(),
        visualDensity: VisualDensity.standard,
        padding: node.padding.flutterEdgeInsets,
        textStyle: textStyle,
        minimumSize: Size.zero,
        shape: getButtonShape(node, scopedValues),
      );
    case ButtonTypeEnum.text:
      buttonStyle = TextButton.styleFrom(
        foregroundColor: labelColor,
        elevation: elevation ?? node.properties.elevation,
        backgroundColor: buttonColor.toFlutterColor(),
        padding: node.padding.flutterEdgeInsets,
        visualDensity: VisualDensity.standard,
        textStyle: textStyle,
        minimumSize: Size.zero,
        shadowColor: shadowColor.toFlutterColor(),
        shape: getButtonShape(node, scopedValues),
      );
    case ButtonTypeEnum.outlined:
      buttonStyle = OutlinedButton.styleFrom(
        foregroundColor: labelColor,
        elevation: elevation ?? node.properties.elevation,
        side: getButtonShape(node, scopedValues)?.side,
        backgroundColor: Colors.transparent,
        visualDensity: VisualDensity.standard,
        padding: node.padding.flutterEdgeInsets,
        textStyle: textStyle,
        minimumSize: Size.zero,
        shadowColor: shadowColor.toFlutterColor(),
        shape: getButtonShape(node, scopedValues),
      );
    case ButtonTypeEnum.icon:
      Color? primary;
      if (buttonColor.toFlutterColor().opacity > 0 &&
          buttonColor.toFlutterColor().opacity < 0.7) {
        primary = buttonColor.toFlutterColor().withOpacity(0.1);
      } else if (buttonColor.toFlutterColor().opacity == 0) {
        final iconColor = PropertyValueDelegate.getPropertyValue<ColorRGBA>(
              node,
              'iconColor',
              scopedValues: scopedValues,
            ) ??
            node.properties.icon.color;
        primary = iconColor?.toFlutterColor().withOpacity(0.1);
      }
      buttonStyle = TextButton.styleFrom(
        foregroundColor: primary,
        elevation: elevation ?? node.properties.elevation,
        visualDensity: VisualDensity.standard,
        padding: node.padding.flutterEdgeInsets,
        minimumSize: Size.zero,
        backgroundColor: buttonColor.toFlutterColor(),
        shadowColor: shadowColor.toFlutterColor(),
        shape: getButtonShape(node, scopedValues),
      );
  }
  return buttonStyle.copyWith(
      elevation:
          MaterialStateProperty.all(elevation ?? node.properties.elevation));
}