link_btn.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import 'package:flutter/material.dart';
  2. import 'package:link/constants.dart';
  3. import '../../utils/size_fit.dart';
  4. class LinkButton extends StatefulWidget {
  5. String title;
  6. // ignore: prefer_typing_uninitialized_variables
  7. var callback;
  8. bool disable;
  9. bool isBlack;
  10. double width = 310.px;
  11. double height = 48.px;
  12. LinkButton(
  13. {Key? key,
  14. required this.title,
  15. required this.disable,
  16. required this.isBlack,
  17. required this.callback,
  18. double? btnWidth,
  19. double? btnHeight})
  20. : super(key: key) {
  21. if (btnWidth != null) {
  22. width = btnWidth;
  23. }
  24. if (btnHeight != null) {
  25. height = btnHeight;
  26. }
  27. }
  28. @override
  29. State<LinkButton> createState() => _LinkButtonState();
  30. }
  31. class _LinkButtonState extends State<LinkButton>
  32. with SingleTickerProviderStateMixin {
  33. double scale = 1.0;
  34. double alpha = 1.0;
  35. late AnimationController controller;
  36. late Animation animation;
  37. @override
  38. void initState() {
  39. // TODO: implement initState
  40. controller = AnimationController(
  41. vsync: this, duration: const Duration(milliseconds: 100));
  42. animation = Tween(begin: 0.0, end: 1.0)
  43. .animate(CurvedAnimation(parent: controller, curve: Curves.easeOut));
  44. controller.addListener(() {
  45. setState(() {});
  46. });
  47. super.initState();
  48. }
  49. @override
  50. void dispose() {
  51. controller.dispose();
  52. super.dispose();
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. SizeFit.initialize(context);
  57. return GestureDetector(
  58. onTap: () {
  59. if (widget.disable == false) {
  60. widget.callback();
  61. }
  62. },
  63. onTapDown: (TapDownDetails details) {
  64. if (widget.disable == false) {
  65. // setState(() {
  66. // scale = 0.95;
  67. // alpha = 0.8;
  68. // });
  69. controller.forward();
  70. }
  71. },
  72. onTapUp: (TapUpDetails details) {
  73. if (widget.disable == false) {
  74. // setState(() {
  75. // scale = 1.0;
  76. // alpha = 1.0;
  77. // });
  78. controller.reverse();
  79. }
  80. },
  81. onTapCancel: () {
  82. if (widget.disable == false) {
  83. // setState(() {
  84. // scale = 1.0;
  85. // alpha = 1.0;
  86. // });
  87. controller.reverse();
  88. }
  89. },
  90. child: Transform.scale(
  91. scale: 1 - 0.05 * animation.value,
  92. child: Opacity(
  93. opacity: 1 - 0.2 * animation.value, //alpha,
  94. child: Container(
  95. width: widget.width + 8.px,
  96. height: widget.height + 8.px,
  97. alignment: Alignment.center,
  98. child: Container(
  99. alignment: Alignment.center,
  100. width: widget.width,
  101. height: widget.height,
  102. child: Text(
  103. widget.title,
  104. style: TextStyle(
  105. color:
  106. widget.isBlack ? Colors.white : const Color(0xFF131314),
  107. fontSize: 16.px,
  108. fontWeight: FontWeight.bold),
  109. ),
  110. decoration: BoxDecoration(
  111. color: widget.isBlack ? Colors.black : kThemeColor,
  112. borderRadius: BorderRadius.circular(widget.height / 2.0)),
  113. ),
  114. decoration: BoxDecoration(
  115. boxShadow: [
  116. BoxShadow(
  117. color: kBtnColor,
  118. blurRadius: 0.0,
  119. offset: Offset(0, 10.px)),
  120. BoxShadow(
  121. //透明度 从0.4->0.6,blur 20->60
  122. color: const Color(0x66FF9900),
  123. blurRadius: 20.px,
  124. spreadRadius: 6.px,
  125. offset: Offset(0, 20.px))
  126. ],
  127. color: kBtnColor,
  128. borderRadius:
  129. BorderRadius.circular(widget.height / 2.0 + 4.px)),
  130. ),
  131. ),
  132. ),
  133. );
  134. }
  135. }
  136. class AlertButton extends StatefulWidget {
  137. String title;
  138. double width, height;
  139. // ignore: prefer_typing_uninitialized_variables
  140. var callback;
  141. bool isCancel;
  142. AlertButton(
  143. {Key? key,
  144. required this.title,
  145. required this.isCancel,
  146. required this.width,
  147. required this.height,
  148. required this.callback})
  149. : super(key: key);
  150. @override
  151. State<AlertButton> createState() => _AlertButtonState();
  152. }
  153. class _AlertButtonState extends State<AlertButton> {
  154. double scale = 1.0;
  155. double alpha = 1.0;
  156. @override
  157. Widget build(BuildContext context) {
  158. SizeFit.initialize(context);
  159. return GestureDetector(
  160. onTap: () {
  161. widget.callback();
  162. },
  163. onTapDown: (TapDownDetails details) {
  164. setState(() {
  165. scale = 0.95;
  166. alpha = 0.8;
  167. });
  168. },
  169. onTapUp: (TapUpDetails details) {
  170. setState(() {
  171. scale = 1.0;
  172. alpha = 1.0;
  173. });
  174. },
  175. onTapCancel: () {
  176. setState(() {
  177. scale = 1.0;
  178. alpha = 1.0;
  179. });
  180. },
  181. child: Transform.scale(
  182. scale: scale,
  183. child: Opacity(
  184. opacity: alpha,
  185. child: Container(
  186. width: widget.width + 8.px,
  187. height: widget.height + 8.px,
  188. alignment: Alignment.center,
  189. child: Container(
  190. alignment: Alignment.center,
  191. width: widget.width,
  192. height: widget.height,
  193. child: Text(
  194. widget.title,
  195. style: TextStyle(
  196. color: const Color(0xFF131314),
  197. fontSize: 16.px,
  198. fontWeight: FontWeight.bold),
  199. ),
  200. decoration: BoxDecoration(
  201. color:
  202. widget.isCancel ? const Color(0xFFA5A5AD) : kThemeColor,
  203. borderRadius: BorderRadius.circular(widget.height / 2.0)),
  204. ),
  205. decoration: BoxDecoration(
  206. boxShadow: [
  207. BoxShadow(
  208. color: widget.isCancel ? Colors.black : kBtnColor,
  209. blurRadius: 0.0,
  210. offset: Offset(0, 10.px)),
  211. ],
  212. color: widget.isCancel ? Colors.black : kBtnColor,
  213. borderRadius:
  214. BorderRadius.circular(widget.height / 2.0 + 4.px)),
  215. ),
  216. ),
  217. ),
  218. );
  219. }
  220. }