import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'utils/dark_theme_util.dart';
import 'package:shared/utils/ext_dart.dart';

// ignore: slash_for_doc_comments
/**

    MyButton(
    fontSize: 16,
    textColor: Colors.black,
    text: "My Button的封装按钮",
    backgroundColor: ColorConstants.gray,
    onPressed: () {
    SmartDialog.compatible.showToast("MyButton的封装按钮");
    },
    radius: 15,
    side: BorderSide(color: Colors.black,width: 1.0),
    ),

 */
class MyButton extends StatelessWidget {
  const MyButton({
    Key? key,
    required this.onPressed,
    this.text = '',
    this.fontSize = 16,
    this.textColor,
    this.disabledTextColor,
    this.backgroundColor,
    this.disabledBackgroundColor,
    this.minHeight = 43.0,
    this.minWidth = double.infinity,
    this.padding = const EdgeInsets.symmetric(horizontal: 16.0),
    this.radius = 5.0,
    this.enableOverlay = true,
    this.elevation = 0.0,
    this.shadowColor = Colors.black,
    this.side = BorderSide.none,
    this.fontWeight,
    this.type = ClickType.none,
    this.milliseconds = 500,
    this.enable = true, // 控制按钮是否可用
    this.textAlign = TextAlign.center,
  }) : super(key: key);

  final String text;
  final double fontSize;
  final Color? textColor;
  final Color? disabledTextColor;
  final Color? backgroundColor;
  final Color? disabledBackgroundColor;
  final double? minHeight;
  final double? minWidth;
  final VoidCallback? onPressed;
  final EdgeInsetsGeometry padding;
  final double radius;
  final BorderSide side;
  final bool enableOverlay;
  final double elevation;
  final Color? shadowColor;
  final FontWeight? fontWeight;
  final ClickType type;
  final int milliseconds;
  final bool enable; // 控制按钮是否可用
  final TextAlign? textAlign;

  @override
  Widget build(BuildContext context) {
    Color _textColor = textColor ?? Colors.white;
    return TextButton(
        onPressed: enable
            ? (type == ClickType.debounce
            ? onPressed?.debounce(milliseconds)
            : type == ClickType.throttle
            ? onPressed?.throttle(milliseconds)
            : onPressed)
            : (){},
        style: ButtonStyle(
          foregroundColor: MaterialStateProperty.resolveWith((states) {
              if (!enable) {
                return DarkThemeUtil.multiColors(
                    context, disabledTextColor ?? Colors.grey,
                    darkColor: Colors.grey);
              }
              return DarkThemeUtil.multiColors(
                  context, _textColor ,
                  darkColor: textColor??Colors.white);
            },
          ),
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            if (!enable) {
              return disabledBackgroundColor;
            }
            return backgroundColor;
          }),
          overlayColor: MaterialStateProperty.resolveWith((states) {
            return enableOverlay
                ? DarkThemeUtil.multiColors(context, textColor ?? Colors.white)
                ?.withOpacity(0.12)
                : Colors.transparent;
          }),
          minimumSize: (minWidth == null || minHeight == null)
              ? null
              : MaterialStateProperty.all<Size>(
              Size(minWidth!, minHeight!)),
          padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
          shape: MaterialStateProperty.all<OutlinedBorder>(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(radius),
            ),
          ),
          side: MaterialStateProperty.all<BorderSide>(side),
          elevation: MaterialStateProperty.all<double>(elevation),
          shadowColor: MaterialStateProperty.all<Color>(
              DarkThemeUtil.multiColors(
                  context, shadowColor ?? Colors.black,
                  darkColor: Colors.white)!),
        ),
        child: Text(
          text,
          textAlign: textAlign ?? TextAlign.center,
          style: TextStyle(
              fontSize: fontSize,
              fontWeight: fontWeight ?? FontWeight.w400,
          ),
        ));
  }
}

