imageButton.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import 'package:faqim_mobile/components/clickUtil.dart';
  2. import 'package:flutter/material.dart';
  3. class ImageButton extends StatelessWidget {
  4. final Size buttonSize;
  5. final String image;
  6. final Size imageSize;
  7. final Color bgColor;
  8. final double borderRadius;
  9. final Color borderColor;
  10. final double borderWidth;
  11. final VoidCallback? onTap;
  12. ImageButton({
  13. required this.buttonSize,
  14. required this.image,
  15. Size? imageSize,
  16. this.bgColor = Colors.white,
  17. this.borderRadius = 5,
  18. Color? borderColor,
  19. this.borderWidth = 1,
  20. this.onTap,
  21. })
  22. : imageSize = imageSize ?? buttonSize,
  23. borderColor = borderColor ?? bgColor
  24. ;
  25. @override
  26. Widget build(BuildContext context) {
  27. return GestureDetector(
  28. onTap: () {
  29. // 防止快速连击重复触发。
  30. if (ClickUtil.isFastClick()) {
  31. return;
  32. }
  33. onTap?.call();
  34. },
  35. child: Opacity(
  36. opacity: onTap == null ? 0.5 : 1.0,
  37. child: Container(
  38. width: buttonSize.width,
  39. height: buttonSize.height,
  40. decoration: BoxDecoration(
  41. color: bgColor,
  42. shape: BoxShape.rectangle,
  43. borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
  44. border: Border.all(color: borderColor),
  45. ),
  46. child: Center(
  47. child: Image.asset(
  48. image,
  49. width: imageSize.width,
  50. height: imageSize.height,
  51. ),
  52. ),
  53. ),
  54. ),
  55. );
  56. }
  57. }
  58. class ImageColumnTextButton extends StatelessWidget {
  59. final Size buttonSize;
  60. final String image;
  61. final Size imageSize;
  62. final String text;
  63. final TextStyle? textStyle;
  64. final double space;
  65. final Color bgColor;
  66. final double borderRadius;
  67. final Color borderColor;
  68. final double borderWidth;
  69. final VoidCallback? onTap;
  70. ImageColumnTextButton({
  71. required this.buttonSize,
  72. required this.image,
  73. Size? imageSize,
  74. required this.text,
  75. this.textStyle,
  76. this.space = 4,
  77. this.bgColor = Colors.white,
  78. this.borderRadius = 5,
  79. Color? borderColor,
  80. this.borderWidth = 1,
  81. this.onTap,
  82. })
  83. : imageSize = imageSize ?? buttonSize,
  84. borderColor = borderColor ?? bgColor
  85. ;
  86. @override
  87. Widget build(BuildContext context) {
  88. return GestureDetector(
  89. onTap: onTap,
  90. child: Opacity(
  91. opacity: onTap == null ? 0.5 : 1.0,
  92. child: Container(
  93. width: buttonSize.width,
  94. height: buttonSize.height,
  95. decoration: BoxDecoration(
  96. color: bgColor,
  97. shape: BoxShape.rectangle,
  98. borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
  99. border: Border.all(color: borderColor),
  100. ),
  101. child: Center(
  102. child: Column(
  103. mainAxisAlignment: MainAxisAlignment.center,
  104. crossAxisAlignment: CrossAxisAlignment.center,
  105. children: [
  106. Image.asset(
  107. image,
  108. width: imageSize.width,
  109. height: imageSize.height,
  110. ),
  111. SizedBox(height: space),
  112. Text(
  113. text,
  114. style: textStyle,
  115. textAlign: TextAlign.center,
  116. ),
  117. ],
  118. ),
  119. ),
  120. ),
  121. ),
  122. );
  123. }
  124. }
  125. class ImageRowTextButton extends StatelessWidget {
  126. final Size buttonSize;
  127. final String image;
  128. final Size imageSize;
  129. final String text;
  130. final TextStyle? textStyle;
  131. final double space;
  132. final Color bgColor;
  133. final double borderRadius;
  134. final Color borderColor;
  135. final double borderWidth;
  136. final VoidCallback? onTap;
  137. ImageRowTextButton({
  138. required this.buttonSize,
  139. required this.image,
  140. Size? imageSize,
  141. required this.text,
  142. this.textStyle,
  143. this.space = 2,
  144. this.bgColor = Colors.white,
  145. this.borderRadius = 5,
  146. Color? borderColor,
  147. this.borderWidth = 1,
  148. this.onTap,
  149. })
  150. : imageSize = imageSize ?? buttonSize,
  151. borderColor = borderColor ?? bgColor
  152. ;
  153. @override
  154. Widget build(BuildContext context) {
  155. return GestureDetector(
  156. onTap: onTap,
  157. child: Opacity(
  158. opacity: onTap == null ? 0.5 : 1.0,
  159. child: Container(
  160. width: buttonSize.width,
  161. height: buttonSize.height,
  162. decoration: BoxDecoration(
  163. color: bgColor,
  164. shape: BoxShape.rectangle,
  165. borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
  166. border: Border.all(color: borderColor),
  167. ),
  168. child: Center(
  169. child: Row(
  170. mainAxisAlignment: MainAxisAlignment.center,
  171. crossAxisAlignment: CrossAxisAlignment.center,
  172. children: [
  173. Image.asset(
  174. image,
  175. width: imageSize.width,
  176. height: imageSize.height,
  177. ),
  178. SizedBox(width: space),
  179. Text(
  180. text,
  181. style: textStyle,
  182. textAlign: TextAlign.left,
  183. ),
  184. ],
  185. ),
  186. ),
  187. ),
  188. ),
  189. );
  190. }
  191. }