buildButton method

Widget buildButton(
  1. BuildContext context
)

Implementation

Widget buildButton(BuildContext context) {
  if (context.isIOS) {
    final Color finalForegroundColor = foregroundColor ??
        Theme.of(context)
            .textButtonTheme
            .style!
            .foregroundColor!
            .resolve({MaterialState.selected})!;

    final Color finalBackgroundColor = backgroundColor ??
        Theme.of(context)
            .textButtonTheme
            .style!
            .backgroundColor!
            .resolve({MaterialState.selected})!;

    final Color finalDisabledBackgroundColor =
        disabledBackgroundColor ?? finalBackgroundColor.withOpacity(0.3);

    Widget buttonChild = Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        if (icon != null)
          Padding(
            padding: EdgeInsets.only(
              right: text != null && text!.isNotEmpty ? 8 : 0,
            ),
            child: IconTheme(
              data:
                  IconTheme.of(context).copyWith(color: finalForegroundColor),
              child: icon!,
            ),
          ),
        if (text != null && text!.isNotEmpty)
          Text(
            text!,
            style: TextStyle(
              fontFamily: fontFamily(context),
              color: finalForegroundColor,
            ).merge(textStyle),
          ),
      ],
    );

    if (child != null) {
      buttonChild = DefaultTextStyle(
        style: textStyle ?? DefaultTextStyle.of(context).style,
        child: child!,
      );
    }

    return GestureDetector(
      onLongPress: onLongPress,
      child: CupertinoButton(
        color: finalBackgroundColor,
        disabledColor: finalDisabledBackgroundColor,
        minSize: 0,
        padding: padding ??
            const EdgeInsets.symmetric(
              vertical: 14, // Default
              horizontal: 18,
            ),
        onPressed: onPressed,
        borderRadius:
            borderRadius ?? const BorderRadius.all(Radius.circular(8)),
        child: buttonChild,
      ),
    );
  }

  ButtonStyle style = Theme.of(context).textButtonTheme.style!;

  if (foregroundColor != null) {
    style = style.copyWith(
      foregroundColor: MaterialStateProperty.resolveWith((states) {
        if (states.contains(MaterialState.disabled)) {
          return foregroundColor!.withOpacity(0.5);
        }

        return foregroundColor;
      }),
    );
  }

  if (backgroundColor != null) {
    style = style.copyWith(
      backgroundColor: MaterialStateProperty.resolveWith((states) {
        if (states.contains(MaterialState.disabled)) {
          return backgroundColor!.withOpacity(0.5);
        }

        return backgroundColor;
      }),
    );
  }

  if (padding != null) {
    style = style.copyWith(padding: MaterialStateProperty.all(padding));
  }

  if (borderRadius != null) {
    style = style.copyWith(
      shape: MaterialStateProperty.all(
        RoundedRectangleBorder(borderRadius: borderRadius!),
      ),
    );
  }

  if (textStyle != null) {
    style = style.copyWith(
      textStyle: MaterialStateProperty.all(
        style.textStyle!.resolve({MaterialState.selected})!.merge(textStyle),
      ),
    );
  }

  if (child != null) {
    return TextButton(
      style: style,
      onPressed: onPressed,
      onLongPress: onLongPress,
      child: child!,
    );
  }

  if (icon == null) {
    return TextButton(
      style: style,
      onPressed: onPressed,
      onLongPress: onLongPress,
      child: Text(text!, style: textStyle),
    );
  }

  return text == null || text!.isEmpty
      ? TextButton(
          style: style,
          onPressed: onPressed,
          onLongPress: onLongPress,
          child: icon!,
        )
      : TextButton.icon(
          icon: icon!,
          label: Text(text!, style: textStyle),
          style: style,
          onPressed: onPressed,
          onLongPress: onLongPress,
        );
}