my_button.dart 4.1 KB

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