gradient_btn.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. import '../resource/string.gen.dart';
  7. /// 渐变色按钮
  8. class GradientBtn extends StatelessWidget {
  9. /// 纯色颜色
  10. final Color? color;
  11. /// 是否启用
  12. final bool enable;
  13. /// 渐变颜色参数
  14. final List<Color> colors;
  15. /// 渐变方向
  16. final AlignmentGeometry begin;
  17. final AlignmentGeometry end;
  18. /// 按钮圆角半径
  19. final double radius;
  20. /// 内边距
  21. final EdgeInsetsGeometry? padding;
  22. /// 子组件
  23. final Widget child;
  24. /// 点击回调
  25. final VoidCallback onPressed;
  26. const GradientBtn({
  27. super.key,
  28. required this.colors,
  29. this.color,
  30. this.enable = true,
  31. this.begin = Alignment.centerLeft,
  32. this.end = Alignment.centerRight,
  33. this.radius = 50,
  34. this.padding = const EdgeInsets.symmetric(vertical: 14),
  35. required this.child,
  36. required this.onPressed,
  37. });
  38. @override
  39. Widget build(BuildContext context) {
  40. Decoration decoration;
  41. // 优先使用纯色,如果没有纯色,则使用渐变色
  42. if (color != null) {
  43. decoration = BoxDecoration(
  44. color: color,
  45. borderRadius: BorderRadius.all(Radius.circular(radius)),
  46. );
  47. } else {
  48. // 渐变色
  49. decoration = ShapeDecoration(
  50. gradient: LinearGradient(colors: colors, begin: begin, end: end),
  51. shape: RoundedRectangleBorder(
  52. borderRadius: BorderRadius.circular(radius),
  53. ),
  54. );
  55. }
  56. return Center(
  57. child: GestureDetector(
  58. onTap: () {
  59. onPressed();
  60. },
  61. child: Stack(
  62. children: [
  63. Container(
  64. padding: padding,
  65. width: double.maxFinite,
  66. // 渐变背景
  67. decoration: decoration,
  68. child: child,
  69. ),
  70. // 禁用状态的蒙层
  71. Positioned(
  72. left: 0,
  73. top: 0,
  74. right: 0,
  75. bottom: 0,
  76. child: !enable ? _buildDisableMask() : SizedBox.shrink(),
  77. ),
  78. ],
  79. ),
  80. ),
  81. );
  82. }
  83. /// 禁用状态的蒙层
  84. Widget _buildDisableMask() {
  85. return Container(
  86. // 宽和高,都匹配父组件
  87. width: double.infinity,
  88. height: double.infinity,
  89. decoration: BoxDecoration(
  90. color: ColorName.white.withOpacity(0.8),
  91. borderRadius: BorderRadius.all(Radius.circular(radius)),
  92. ),
  93. );
  94. }
  95. }
  96. /// 渐变按钮,中间为文字,文字左边是一个图标
  97. class GradientTextBtn extends StatelessWidget {
  98. /// 文字左边的图标
  99. final Image? leftIcon;
  100. /// 按钮文字
  101. final String text;
  102. /// 是否启用
  103. final bool enable;
  104. /// 描述文字
  105. final String? desc;
  106. /// 纯色颜色
  107. final Color? color;
  108. /// 渐变颜色参数
  109. final List<Color> colors;
  110. /// 渐变方向
  111. final AlignmentGeometry begin;
  112. final AlignmentGeometry end;
  113. /// 按钮圆角半径
  114. final double radius;
  115. /// 内边距
  116. final EdgeInsetsGeometry? padding;
  117. /// 点击回调
  118. final VoidCallback onPressed;
  119. /// 操作按钮,带解锁按钮
  120. /// [isUnlock] 是否已解锁
  121. static GradientTextBtn withUnlock(
  122. bool isUnlock, {
  123. String? text,
  124. Color? color,
  125. String? desc,
  126. double radius = 50,
  127. EdgeInsetsGeometry? padding = const EdgeInsets.symmetric(vertical: 14),
  128. required VoidCallback onPressed,
  129. }) {
  130. String btnText = text ?? "";
  131. if (btnText.isEmpty) {
  132. btnText = StringName.intimacyUnlockAnalyse;
  133. }
  134. // 解锁图标,非VIP会员时,才显示锁图标
  135. Image? leftIcon;
  136. if (!isUnlock) {
  137. leftIcon = Assets.images.iconIntimacyAnalyseUnlock.image(
  138. width: 22,
  139. height: 22,
  140. );
  141. }
  142. return GradientTextBtn(
  143. btnText,
  144. desc: desc,
  145. color: color ?? ColorName.colorBrand,
  146. leftIcon: leftIcon,
  147. radius: radius,
  148. padding: padding,
  149. onPressed: onPressed,
  150. );
  151. }
  152. const GradientTextBtn(
  153. this.text, {
  154. super.key,
  155. this.leftIcon,
  156. this.desc,
  157. this.color,
  158. this.enable = true,
  159. this.colors = const [ColorName.purpleGradient3, ColorName.purpleGradient4],
  160. this.begin = Alignment.centerLeft,
  161. this.end = Alignment.centerRight,
  162. this.radius = 50,
  163. this.padding = const EdgeInsets.symmetric(vertical: 14),
  164. required this.onPressed,
  165. });
  166. @override
  167. Widget build(BuildContext context) {
  168. String descStr = desc ?? "";
  169. return GradientBtn(
  170. color: color,
  171. colors: colors,
  172. radius: radius,
  173. onPressed: onPressed,
  174. padding: padding,
  175. enable: enable,
  176. child: Column(
  177. children: [
  178. Row(
  179. mainAxisAlignment: MainAxisAlignment.center,
  180. children: [
  181. // 左侧图标
  182. Visibility(
  183. visible: leftIcon != null,
  184. child: Container(
  185. margin: EdgeInsets.only(right: 4.w),
  186. child: leftIcon,
  187. ),
  188. ),
  189. // 文字
  190. Text(
  191. text,
  192. style: TextStyle(
  193. color: ColorName.white,
  194. fontSize: 16.sp,
  195. fontWeight: FontWeight.bold,
  196. ),
  197. ),
  198. ],
  199. ),
  200. // 描述
  201. Visibility(
  202. visible: descStr.isNotEmpty,
  203. child: Container(
  204. margin: EdgeInsets.only(top: 3.h),
  205. child: Text(
  206. descStr,
  207. style: TextStyle(
  208. color: ColorName.white,
  209. fontSize: 10.sp,
  210. fontWeight: FontWeight.w400,
  211. ),
  212. ),
  213. ),
  214. ),
  215. ],
  216. ),
  217. );
  218. }
  219. }