button.dart 3.1 KB

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