gradient_btn.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import '../resource/assets.gen.dart';
  5. import '../resource/colors.gen.dart';
  6. /// 渐变色按钮
  7. class GradientBtn extends StatelessWidget {
  8. /// 纯色颜色
  9. final Color? color;
  10. /// 渐变颜色参数
  11. final List<Color> colors;
  12. /// 渐变方向
  13. final AlignmentGeometry begin;
  14. final AlignmentGeometry end;
  15. /// 按钮圆角半径
  16. final double radius;
  17. /// 子组件
  18. final Widget child;
  19. /// 点击回调
  20. final VoidCallback onPressed;
  21. const GradientBtn({
  22. super.key,
  23. required this.colors,
  24. this.color,
  25. this.begin = Alignment.centerLeft,
  26. this.end = Alignment.centerRight,
  27. this.radius = 50,
  28. required this.child,
  29. required this.onPressed,
  30. });
  31. @override
  32. Widget build(BuildContext context) {
  33. Decoration decoration;
  34. // 优先使用纯色,如果没有纯色,则使用渐变色
  35. if (color != null) {
  36. decoration = BoxDecoration(
  37. color: color,
  38. borderRadius: BorderRadius.all(Radius.circular(radius)),
  39. );
  40. } else {
  41. // 渐变色
  42. decoration = ShapeDecoration(
  43. gradient: LinearGradient(colors: colors, begin: begin, end: end),
  44. shape: RoundedRectangleBorder(
  45. borderRadius: BorderRadius.circular(radius),
  46. ),
  47. );
  48. }
  49. return GestureDetector(
  50. onTap: () {
  51. onPressed();
  52. },
  53. child: Container(
  54. padding: EdgeInsets.symmetric(vertical: 14.h),
  55. width: double.maxFinite,
  56. // 渐变背景
  57. decoration: decoration,
  58. child: Center(child: child),
  59. ),
  60. );
  61. }
  62. }
  63. /// 渐变按钮,中间为文字,文字左边是一个图标
  64. class GradientTextBtn extends StatelessWidget {
  65. /// 文字左边的图标
  66. final Image? leftIcon;
  67. /// 按钮文字
  68. final String text;
  69. /// 纯色颜色
  70. final Color? color;
  71. /// 渐变颜色参数
  72. final List<Color> colors;
  73. /// 渐变方向
  74. final AlignmentGeometry begin;
  75. final AlignmentGeometry end;
  76. /// 按钮圆角半径
  77. final double radius;
  78. /// 点击回调
  79. final VoidCallback onPressed;
  80. const GradientTextBtn(
  81. this.text, {
  82. super.key,
  83. this.leftIcon,
  84. this.color,
  85. this.colors = const [ColorName.purpleGradient3, ColorName.purpleGradient4],
  86. this.begin = Alignment.centerLeft,
  87. this.end = Alignment.centerRight,
  88. this.radius = 50,
  89. required this.onPressed,
  90. });
  91. @override
  92. Widget build(BuildContext context) {
  93. return GradientBtn(
  94. color: color,
  95. colors: colors,
  96. radius: radius,
  97. onPressed: onPressed,
  98. child: Row(
  99. mainAxisAlignment: MainAxisAlignment.center,
  100. children: [
  101. // 左侧图标
  102. Visibility(
  103. visible: leftIcon != null,
  104. child: Container(
  105. margin: EdgeInsets.only(right: 4.w),
  106. child: leftIcon,
  107. ),
  108. ),
  109. Text(
  110. text,
  111. style: TextStyle(
  112. color: ColorName.white,
  113. fontSize: 16.sp,
  114. fontWeight: FontWeight.bold,
  115. ),
  116. ),
  117. ],
  118. ),
  119. );
  120. }
  121. }