my_button.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. Color _textColor = textColor ?? Colors.white;
  68. return TextButton(
  69. onPressed: enable
  70. ? (type == ClickType.debounce
  71. ? onPressed?.debounce(milliseconds)
  72. : type == ClickType.throttle
  73. ? onPressed?.throttle(milliseconds)
  74. : onPressed)
  75. : (){},
  76. style: ButtonStyle(
  77. foregroundColor: MaterialStateProperty.resolveWith((states) {
  78. if (!enable) {
  79. return DarkThemeUtil.multiColors(
  80. context, disabledTextColor ?? Colors.grey,
  81. darkColor: Colors.grey);
  82. }
  83. return DarkThemeUtil.multiColors(
  84. context, _textColor ,
  85. darkColor: textColor??Colors.white);
  86. },
  87. ),
  88. backgroundColor: MaterialStateProperty.resolveWith((states) {
  89. if (!enable) {
  90. return disabledBackgroundColor;
  91. }
  92. return backgroundColor;
  93. }),
  94. overlayColor: MaterialStateProperty.resolveWith((states) {
  95. return enableOverlay
  96. ? DarkThemeUtil.multiColors(context, textColor ?? Colors.white)
  97. ?.withOpacity(0.12)
  98. : Colors.transparent;
  99. }),
  100. minimumSize: (minWidth == null || minHeight == null)
  101. ? null
  102. : MaterialStateProperty.all<Size>(
  103. Size(minWidth!, minHeight!)),
  104. padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
  105. shape: MaterialStateProperty.all<OutlinedBorder>(
  106. RoundedRectangleBorder(
  107. borderRadius: BorderRadius.circular(radius),
  108. ),
  109. ),
  110. side: MaterialStateProperty.all<BorderSide>(side),
  111. elevation: MaterialStateProperty.all<double>(elevation),
  112. shadowColor: MaterialStateProperty.all<Color>(
  113. DarkThemeUtil.multiColors(
  114. context, shadowColor ?? Colors.black,
  115. darkColor: Colors.white)!),
  116. ),
  117. child: Text(
  118. text,
  119. textAlign: textAlign ?? TextAlign.center,
  120. style: TextStyle(
  121. fontSize: fontSize,
  122. fontWeight: fontWeight ?? FontWeight.w400,
  123. ),
  124. ));
  125. }
  126. }