my_button.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, //最高高度,默认43
  30. this.minWidth = double.infinity, //最小宽度,默认充满控件
  31. this.padding = const EdgeInsets.symmetric(horizontal: 16.0), //内间距,默认是横向内间距
  32. this.radius = 5.0, //圆角
  33. this.enableOverlay = true, //是否支持水波纹效果,不过这个效果对比InkWell比较克制,推荐开启
  34. this.elevation = 0.0, //是否支持阴影,设置Z轴高度
  35. this.shadowColor = Colors.black, //阴影的颜色
  36. this.side = BorderSide.none, //边框的设置
  37. this.fontWeight,
  38. this.type = ClickType.none,
  39. this.milliseconds = 500,
  40. }) : super(key: key);
  41. final String text;
  42. final double fontSize;
  43. final Color? textColor;
  44. final Color? disabledTextColor;
  45. final Color? backgroundColor;
  46. final Color? disabledBackgroundColor;
  47. final double? minHeight;
  48. final double? minWidth;
  49. final VoidCallback? onPressed;
  50. final EdgeInsetsGeometry padding;
  51. final double radius;
  52. final BorderSide side;
  53. final bool enableOverlay;
  54. final double elevation;
  55. final Color? shadowColor;
  56. final FontWeight? fontWeight;
  57. final ClickType type; //默认没有点击类型
  58. final int milliseconds; //点击类型的时间戳(毫秒)
  59. @override
  60. Widget build(BuildContext context) {
  61. return TextButton(
  62. onPressed: type == ClickType.debounce
  63. ? onPressed?.debounce(milliseconds)
  64. : type == ClickType.throttle
  65. ? onPressed?.throttle(milliseconds)
  66. : onPressed,
  67. style: ButtonStyle(
  68. // 文字颜色
  69. // MaterialStateProperty.all //各种状态都是这个颜色
  70. foregroundColor: MaterialStateProperty.resolveWith(
  71. //根据不同的状态展示不同的颜色
  72. (states) {
  73. if (states.contains(MaterialState.disabled)) {
  74. return DarkThemeUtil.multiColors(context,disabledTextColor ?? Colors.grey, darkColor: Colors.grey);
  75. }
  76. return DarkThemeUtil.multiColors(context,textColor ?? Colors.white, darkColor: Colors.white);
  77. },
  78. ),
  79. // 背景颜色
  80. backgroundColor: MaterialStateProperty.resolveWith((states) {
  81. if (states.contains(MaterialState.disabled)) {
  82. return disabledBackgroundColor;
  83. }
  84. return backgroundColor;
  85. }),
  86. // 水波纹
  87. overlayColor: MaterialStateProperty.resolveWith((states) {
  88. return enableOverlay ? DarkThemeUtil.multiColors(context,textColor ?? Colors.white)?.withOpacity(0.12) : Colors.transparent;
  89. }),
  90. // 按钮最小大小
  91. minimumSize: (minWidth == null || minHeight == null) ? null : MaterialStateProperty.all<Size>(Size(minWidth!, minHeight!)),
  92. padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
  93. shape: MaterialStateProperty.all<OutlinedBorder>(
  94. RoundedRectangleBorder(
  95. borderRadius: BorderRadius.circular(radius),
  96. ),
  97. ),
  98. side: MaterialStateProperty.all<BorderSide>(side),
  99. elevation: MaterialStateProperty.all<double>(elevation),
  100. shadowColor: MaterialStateProperty.all<Color>(DarkThemeUtil.multiColors(context,shadowColor ?? Colors.black, darkColor: Colors.white)!),
  101. ),
  102. child: Text(
  103. text,
  104. style: TextStyle(fontSize: fontSize, fontWeight: fontWeight ?? FontWeight.w400),
  105. ));
  106. }
  107. }