gradient_btn.dart 2.7 KB

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