my_button.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:ftrecruiter/comm/constants/color_constants.dart';
  4. import 'package:ftrecruiter/comm/utils/dark_theme_util.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 = 48.0, //最高高度,默认48
  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. }) : super(key: key);
  38. final String text;
  39. final double fontSize;
  40. final Color? textColor;
  41. final Color? disabledTextColor;
  42. final Color? backgroundColor;
  43. final Color? disabledBackgroundColor;
  44. final double? minHeight;
  45. final double? minWidth;
  46. final VoidCallback? onPressed;
  47. final EdgeInsetsGeometry padding;
  48. final double radius;
  49. final BorderSide side;
  50. final bool enableOverlay;
  51. final double elevation;
  52. final Color? shadowColor;
  53. @override
  54. Widget build(BuildContext context) {
  55. return TextButton(
  56. onPressed: onPressed,
  57. style: ButtonStyle(
  58. // 文字颜色
  59. // MaterialStateProperty.all //各种状态都是这个颜色
  60. foregroundColor: MaterialStateProperty.resolveWith(
  61. //根据不同的状态展示不同的颜色
  62. (states) {
  63. if (states.contains(MaterialState.disabled)) {
  64. return DarkThemeUtil.multiColors(disabledTextColor ?? Colors.grey,
  65. darkColor: Colors.grey);
  66. }
  67. return DarkThemeUtil.multiColors(textColor ?? Colors.white, darkColor: Colors.white);
  68. },
  69. ),
  70. // 背景颜色
  71. backgroundColor: MaterialStateProperty.resolveWith((states) {
  72. if (states.contains(MaterialState.disabled)) {
  73. return DarkThemeUtil.multiColors(disabledBackgroundColor ?? Colors.white,
  74. darkColor: Colors.lightBlue);
  75. }
  76. return DarkThemeUtil.multiColors(backgroundColor ?? Colors.white,
  77. darkColor: ColorConstants.appBlue);
  78. }),
  79. // 水波纹
  80. overlayColor: MaterialStateProperty.resolveWith((states) {
  81. return enableOverlay
  82. ? DarkThemeUtil.multiColors(textColor ?? Colors.white).withOpacity(0.12)
  83. : Colors.transparent;
  84. }),
  85. // 按钮最小大小
  86. minimumSize: (minWidth == null || minHeight == null)
  87. ? null
  88. : MaterialStateProperty.all<Size>(Size(minWidth!, minHeight!)),
  89. padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
  90. shape: MaterialStateProperty.all<OutlinedBorder>(
  91. RoundedRectangleBorder(
  92. borderRadius: BorderRadius.circular(radius),
  93. ),
  94. ),
  95. side: MaterialStateProperty.all<BorderSide>(side),
  96. elevation: MaterialStateProperty.all<double>(elevation),
  97. shadowColor: MaterialStateProperty.all<Color>(
  98. DarkThemeUtil.multiColors(shadowColor ?? Colors.black, darkColor: Colors.white)),
  99. ),
  100. child: Text(
  101. text,
  102. style: TextStyle(fontSize: fontSize),
  103. ));
  104. }
  105. }