my_button.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.enable = 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 enable; // 控制按钮是否可用
  61. @override
  62. Widget build(BuildContext context) {
  63. return TextButton(
  64. onPressed: enable
  65. ? (type == ClickType.debounce
  66. ? onPressed?.debounce(milliseconds)
  67. : type == ClickType.throttle
  68. ? onPressed?.throttle(milliseconds)
  69. : onPressed)
  70. : (){},
  71. style: ButtonStyle(
  72. foregroundColor: MaterialStateProperty.resolveWith((states) {
  73. if (!enable) {
  74. return DarkThemeUtil.multiColors(
  75. context, disabledTextColor ?? Colors.grey,
  76. darkColor: Colors.grey);
  77. }
  78. return DarkThemeUtil.multiColors(
  79. context, textColor ?? Colors.white,
  80. darkColor: Colors.white);
  81. },
  82. ),
  83. backgroundColor: MaterialStateProperty.resolveWith((states) {
  84. if (!enable) {
  85. return disabledBackgroundColor;
  86. }
  87. return backgroundColor;
  88. }),
  89. overlayColor: MaterialStateProperty.resolveWith((states) {
  90. return enableOverlay
  91. ? DarkThemeUtil.multiColors(context, textColor ?? Colors.white)
  92. ?.withOpacity(0.12)
  93. : Colors.transparent;
  94. }),
  95. minimumSize: (minWidth == null || minHeight == null)
  96. ? null
  97. : MaterialStateProperty.all<Size>(
  98. Size(minWidth!, minHeight!)),
  99. padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
  100. shape: MaterialStateProperty.all<OutlinedBorder>(
  101. RoundedRectangleBorder(
  102. borderRadius: BorderRadius.circular(radius),
  103. ),
  104. ),
  105. side: MaterialStateProperty.all<BorderSide>(side),
  106. elevation: MaterialStateProperty.all<double>(elevation),
  107. shadowColor: MaterialStateProperty.all<Color>(
  108. DarkThemeUtil.multiColors(
  109. context, shadowColor ?? Colors.black,
  110. darkColor: Colors.white)!),
  111. ),
  112. child: Text(
  113. text,
  114. style: TextStyle(
  115. fontSize: fontSize, fontWeight: fontWeight ?? FontWeight.w400),
  116. ));
  117. }
  118. }