my_button.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:flutter/material.dart';
  2. import 'package:widgets/ext/ex_widget.dart';
  3. import 'utils/dark_theme_util.dart';
  4. import 'package:shared/utils/ext_dart.dart';
  5. // ignore: slash_for_doc_comments
  6. /**
  7. MyButton(
  8. fontSize: 16,
  9. textColor: Colors.black,
  10. text: "My Button的封装按钮",
  11. backgroundColor: ColorConstants.gray,
  12. onPressed: () {
  13. SmartDialog.compatible.showToast("MyButton的封装按钮");
  14. },
  15. radius: 15,
  16. side: BorderSide(color: Colors.black,width: 1.0),
  17. ),
  18. */
  19. class MyButton extends StatelessWidget {
  20. const MyButton({
  21. Key? key,
  22. required this.onPressed,
  23. this.text = '',
  24. this.fontSize = 16,
  25. this.textColor,
  26. this.disabledTextColor,
  27. this.backgroundColor,
  28. this.disabledBackgroundColor,
  29. this.minHeight = 43.0,
  30. this.minWidth = double.infinity,
  31. this.padding = const EdgeInsets.symmetric(horizontal: 16.0),
  32. this.radius = 5.0,
  33. this.enableOverlay = true,
  34. this.elevation = 0.0,
  35. this.shadowColor = Colors.black,
  36. this.side = BorderSide.none,
  37. this.fontWeight,
  38. this.type = ClickType.none,
  39. this.milliseconds = 500,
  40. this.isEnabled = true, // 控制按钮是否可用
  41. }) : super(key: key);
  42. final String text;
  43. final double fontSize;
  44. final Color? textColor;
  45. final Color? disabledTextColor;
  46. final Color? backgroundColor;
  47. final Color? disabledBackgroundColor;
  48. final double? minHeight;
  49. final double? minWidth;
  50. final VoidCallback? onPressed;
  51. final EdgeInsetsGeometry padding;
  52. final double radius;
  53. final BorderSide side;
  54. final bool enableOverlay;
  55. final double elevation;
  56. final Color? shadowColor;
  57. final FontWeight? fontWeight;
  58. final ClickType type;
  59. final int milliseconds;
  60. final bool isEnabled; // 控制按钮是否可用
  61. @override
  62. Widget build(BuildContext context) {
  63. return TextButton(
  64. onPressed: isEnabled
  65. ? (type == ClickType.debounce
  66. ? onPressed?.debounce(milliseconds)
  67. : type == ClickType.throttle
  68. ? onPressed?.throttle(milliseconds)
  69. : onPressed)
  70. : null, // 如果按钮不可用,则 onPressed 设为 null
  71. style: ButtonStyle(
  72. foregroundColor: MaterialStateProperty.resolveWith(
  73. (states) {
  74. if (states.contains(MaterialState.disabled)) {
  75. return DarkThemeUtil.multiColors(
  76. context, disabledTextColor ?? Colors.grey,
  77. darkColor: Colors.grey);
  78. }
  79. return DarkThemeUtil.multiColors(
  80. context, textColor ?? Colors.white,
  81. darkColor: Colors.white);
  82. },
  83. ),
  84. backgroundColor: MaterialStateProperty.resolveWith((states) {
  85. if (states.contains(MaterialState.disabled)) {
  86. return disabledBackgroundColor;
  87. }
  88. return backgroundColor;
  89. }),
  90. overlayColor: MaterialStateProperty.resolveWith((states) {
  91. return enableOverlay
  92. ? DarkThemeUtil.multiColors(context, textColor ?? Colors.white)
  93. ?.withOpacity(0.12)
  94. : Colors.transparent;
  95. }),
  96. minimumSize: (minWidth == null || minHeight == null)
  97. ? null
  98. : MaterialStateProperty.all<Size>(
  99. Size(minWidth!, minHeight!)),
  100. padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
  101. shape: MaterialStateProperty.all<OutlinedBorder>(
  102. RoundedRectangleBorder(
  103. borderRadius: BorderRadius.circular(radius),
  104. ),
  105. ),
  106. side: MaterialStateProperty.all<BorderSide>(side),
  107. elevation: MaterialStateProperty.all<double>(elevation),
  108. shadowColor: MaterialStateProperty.all<Color>(
  109. DarkThemeUtil.multiColors(
  110. context, shadowColor ?? Colors.black,
  111. darkColor: Colors.white)!),
  112. ),
  113. child: Text(
  114. text,
  115. style: TextStyle(
  116. fontSize: fontSize, fontWeight: fontWeight ?? FontWeight.w400),
  117. ));
  118. }
  119. }