gradient_btn.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 EdgeInsetsGeometry? leftIconMargin;
  102. /// 按钮文字
  103. final String text;
  104. /// 是否启用
  105. final bool enable;
  106. /// 描述文字
  107. final String? desc;
  108. /// 纯色颜色
  109. final Color? color;
  110. /// 渐变颜色参数
  111. final List<Color> colors;
  112. /// 渐变方向
  113. final AlignmentGeometry begin;
  114. final AlignmentGeometry end;
  115. /// 按钮圆角半径
  116. final double radius;
  117. /// 内边距
  118. final EdgeInsetsGeometry? padding;
  119. /// 点击回调
  120. final VoidCallback onPressed;
  121. /// 操作按钮,带解锁按钮
  122. /// [isUnlock] 是否已解锁
  123. static GradientTextBtn withUnlock(
  124. bool isUnlock, {
  125. Image? leftIcon,
  126. EdgeInsetsGeometry? leftIconMargin = const EdgeInsets.only(right: 4),
  127. String? text,
  128. List<Color>? colors,
  129. Color? color,
  130. String? desc,
  131. double radius = 50,
  132. EdgeInsetsGeometry? padding = const EdgeInsets.symmetric(vertical: 14),
  133. required VoidCallback onPressed,
  134. }) {
  135. String btnText = text ?? "";
  136. if (btnText.isEmpty) {
  137. btnText = StringName.intimacyUnlockAnalyse;
  138. }
  139. // 解锁图标,非VIP会员时,才显示锁图标
  140. if (!isUnlock) {
  141. leftIcon ??= Assets.images.iconIntimacyAnalyseUnlock.image(
  142. width: 22,
  143. height: 22,
  144. );
  145. }
  146. // 渐变色
  147. if (colors != null) {
  148. return GradientTextBtn(
  149. btnText,
  150. desc: desc,
  151. colors: colors,
  152. leftIcon: leftIcon,
  153. leftIconMargin: leftIconMargin,
  154. radius: radius,
  155. padding: padding,
  156. onPressed: onPressed,
  157. );
  158. } else {
  159. // 纯色
  160. return GradientTextBtn(
  161. btnText,
  162. desc: desc,
  163. color: color ?? ColorName.colorBrand,
  164. leftIcon: leftIcon,
  165. leftIconMargin: leftIconMargin,
  166. radius: radius,
  167. padding: padding,
  168. onPressed: onPressed,
  169. );
  170. }
  171. }
  172. const GradientTextBtn(
  173. this.text, {
  174. super.key,
  175. this.leftIcon,
  176. this.leftIconMargin,
  177. this.desc,
  178. this.color,
  179. this.enable = true,
  180. this.colors = const [ColorName.purpleGradient3, ColorName.purpleGradient4],
  181. this.begin = Alignment.centerLeft,
  182. this.end = Alignment.centerRight,
  183. this.radius = 50,
  184. this.padding = const EdgeInsets.symmetric(vertical: 14),
  185. required this.onPressed,
  186. });
  187. @override
  188. Widget build(BuildContext context) {
  189. String descStr = desc ?? "";
  190. return GradientBtn(
  191. color: color,
  192. colors: colors,
  193. radius: radius,
  194. onPressed: onPressed,
  195. padding: padding,
  196. enable: enable,
  197. child: Column(
  198. children: [
  199. Row(
  200. mainAxisAlignment: MainAxisAlignment.center,
  201. children: [
  202. // 左侧图标
  203. Visibility(
  204. visible: leftIcon != null,
  205. child: Container(margin: leftIconMargin, child: leftIcon),
  206. ),
  207. // 文字
  208. Text(
  209. text,
  210. style: TextStyle(
  211. color: ColorName.white,
  212. fontSize: 16.sp,
  213. fontWeight: FontWeight.bold,
  214. ),
  215. ),
  216. ],
  217. ),
  218. // 描述
  219. Visibility(
  220. visible: descStr.isNotEmpty,
  221. child: Container(
  222. margin: EdgeInsets.only(top: 3.h),
  223. child: Text(
  224. descStr,
  225. style: TextStyle(
  226. color: ColorName.white,
  227. fontSize: 10.sp,
  228. fontWeight: FontWeight.w400,
  229. ),
  230. ),
  231. ),
  232. ),
  233. ],
  234. ),
  235. );
  236. }
  237. }