button.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:flutter/material.dart';
  2. import 'package:fis_common/helpers/color.dart';
  3. import 'package:vitalapp/architecture/utils/common_util.dart';
  4. enum VButtonType {
  5. primary,
  6. error,
  7. cancel,
  8. text,
  9. }
  10. class VButton extends StatelessWidget {
  11. /// 类型
  12. final VButtonType type;
  13. /// 文字
  14. final String? label;
  15. /// 自定义子组件
  16. final Widget? child;
  17. /// 点击事件回调
  18. final VoidCallback? onTap;
  19. const VButton({
  20. super.key,
  21. this.type = VButtonType.primary,
  22. this.label,
  23. this.child,
  24. this.onTap,
  25. }) : assert(label != null || child != null);
  26. @override
  27. Widget build(BuildContext context) {
  28. final colors = _findBgColor(context, type);
  29. final bgColor = colors.bgColor;
  30. final textColor = colors.textColor;
  31. return LayoutBuilder(builder: (context, c) {
  32. double width = c.maxWidth;
  33. if (width > 1200) {
  34. width = 270;
  35. }
  36. double height = c.maxHeight;
  37. if (height > 720) {
  38. height = 60;
  39. }
  40. double radius = height / 4;
  41. List<Color> havaColors = [
  42. // Color.fromRGBO(59, 188, 255, 1),
  43. // Color.fromRGBO(44, 120, 229, 1),
  44. FColorHelper.mixColor(Colors.white, Theme.of(context).primaryColor, 40),
  45. FColorHelper.mixColor(Colors.black, Theme.of(context).primaryColor, 5),
  46. ];
  47. return Container(
  48. width: 270,
  49. height: height,
  50. decoration: BoxDecoration(
  51. borderRadius: BorderRadius.all(Radius.circular(radius)),
  52. gradient: LinearGradient(
  53. begin: Alignment.topRight,
  54. end: Alignment.bottomLeft,
  55. colors: havaColors,
  56. ),
  57. ),
  58. child: ElevatedButton(
  59. onPressed: () {
  60. CommonUtil.debounce(() {
  61. onTap?.call();
  62. }, durationTime: 300);
  63. },
  64. style: ButtonStyle(
  65. foregroundColor: MaterialStatePropertyAll(textColor),
  66. backgroundColor: const MaterialStatePropertyAll(Colors.transparent),
  67. elevation: const MaterialStatePropertyAll(0),
  68. padding: MaterialStatePropertyAll(
  69. EdgeInsets.symmetric(horizontal: radius, vertical: 8),
  70. ),
  71. alignment: Alignment.center,
  72. shape: MaterialStatePropertyAll(
  73. RoundedRectangleBorder(
  74. borderRadius: BorderRadius.circular(radius),
  75. ),
  76. ),
  77. ),
  78. child: child ??
  79. Text(
  80. label!,
  81. style: TextStyle(
  82. color: textColor,
  83. fontSize: 18,
  84. ),
  85. ),
  86. ),
  87. );
  88. });
  89. }
  90. _BgColorTuple _findBgColor(BuildContext context, VButtonType type) {
  91. switch (type) {
  92. case VButtonType.primary:
  93. return _BgColorTuple(Theme.of(context).primaryColor, Colors.white);
  94. case VButtonType.error:
  95. return _BgColorTuple(Theme.of(context).colorScheme.error, Colors.white);
  96. case VButtonType.text:
  97. return _BgColorTuple(Colors.white, Theme.of(context).primaryColor);
  98. case VButtonType.cancel:
  99. return _BgColorTuple(Colors.grey, Colors.white);
  100. }
  101. }
  102. }
  103. class _BgColorTuple {
  104. final Color bgColor;
  105. final Color textColor;
  106. _BgColorTuple(this.bgColor, this.textColor);
  107. }