gradient_btn.dart 6.5 KB

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