button.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * @Author: XianKaiQun
  3. * @Date: 2020-09-28 11:13:31
  4. * @LastEditors : WuWei
  5. * @LastEditTime : 2023-05-18 15:42:12
  6. * @Description:
  7. */
  8. import 'package:flutter/material.dart';
  9. import 'loading.dart';
  10. class WButton extends StatefulWidget {
  11. WButton({
  12. Key? key,
  13. required this.onPressed,
  14. this.onLongPress,
  15. this.onHighlightChanged,
  16. this.mouseCursor,
  17. this.textTheme,
  18. this.textColor,
  19. this.disabledTextColor,
  20. this.color,
  21. this.disabledColor,
  22. this.focusColor,
  23. this.hoverColor,
  24. this.highlightColor,
  25. this.splashColor,
  26. this.colorBrightness,
  27. this.elevation = 0,
  28. this.focusElevation = 0,
  29. this.hoverElevation = 0,
  30. this.highlightElevation = 0,
  31. this.disabledElevation = 0,
  32. this.padding,
  33. this.margin,
  34. this.visualDensity,
  35. this.shape,
  36. this.clipBehavior = Clip.none,
  37. this.focusNode,
  38. this.autofocus = false,
  39. this.materialTapTargetSize,
  40. this.animationDuration,
  41. this.minWidth,
  42. this.height,
  43. this.enableFeedback = true,
  44. this.child,
  45. this.focusOnGesture = true,
  46. this.boxShadow,
  47. }) : super(key: key);
  48. final VoidCallback onPressed;
  49. final VoidCallback? onLongPress;
  50. final ValueChanged<bool>? onHighlightChanged;
  51. final boxShadow;
  52. ///手势获取焦点
  53. final bool? focusOnGesture;
  54. final MouseCursor? mouseCursor;
  55. final ButtonTextTheme? textTheme;
  56. final Color? textColor;
  57. final Color? disabledTextColor;
  58. final Color? color;
  59. final Color? disabledColor;
  60. final Color? focusColor;
  61. final Color? hoverColor;
  62. final Color? highlightColor;
  63. final Color? splashColor;
  64. final Brightness? colorBrightness;
  65. final double elevation;
  66. final double focusElevation;
  67. final double hoverElevation;
  68. final double highlightElevation;
  69. final double disabledElevation;
  70. final EdgeInsetsGeometry? padding;
  71. final EdgeInsetsGeometry? margin;
  72. final VisualDensity? visualDensity;
  73. final ShapeBorder? shape;
  74. final Clip clipBehavior;
  75. final FocusNode? focusNode;
  76. final bool autofocus;
  77. final MaterialTapTargetSize? materialTapTargetSize;
  78. final Duration? animationDuration;
  79. final double? minWidth;
  80. final double? height;
  81. final bool enableFeedback;
  82. final Widget? child;
  83. @override
  84. _WButtonState createState() => _WButtonState();
  85. }
  86. class _WButtonState extends State<WButton> {
  87. FocusNode? focusNode;
  88. @override
  89. void initState() {
  90. focusNode = widget.focusNode ?? FocusNode();
  91. super.initState();
  92. }
  93. @override
  94. void dispose() {
  95. focusNode?.dispose();
  96. super.dispose();
  97. }
  98. VoidCallback get onPressed {
  99. return () {
  100. widget.onPressed();
  101. if (widget.focusOnGesture == true) {
  102. focusNode!.requestFocus();
  103. }
  104. };
  105. }
  106. VoidCallback? get onLongPress {
  107. if (widget.onLongPress != null) {
  108. return () {
  109. widget.onLongPress!();
  110. if (widget.focusOnGesture == true) {
  111. focusNode!.requestFocus();
  112. }
  113. };
  114. } else {
  115. return null;
  116. }
  117. }
  118. @override
  119. Widget build(BuildContext context) {
  120. Widget current = MaterialButton(
  121. onPressed: onPressed,
  122. onLongPress: onLongPress,
  123. onHighlightChanged: widget.onHighlightChanged,
  124. mouseCursor: widget.mouseCursor,
  125. textTheme: widget.textTheme,
  126. textColor: widget.textColor,
  127. disabledTextColor: widget.disabledTextColor,
  128. color: widget.color ?? Theme.of(context).primaryColor,
  129. disabledColor: widget.disabledColor ?? Theme.of(context).disabledColor,
  130. focusColor: widget.focusColor,
  131. hoverColor: widget.hoverColor,
  132. highlightColor: widget.highlightColor,
  133. splashColor: widget.splashColor,
  134. colorBrightness: widget.colorBrightness,
  135. elevation: widget.elevation,
  136. focusElevation: widget.focusElevation,
  137. hoverElevation: widget.hoverElevation,
  138. highlightElevation: widget.highlightElevation,
  139. disabledElevation: widget.disabledElevation,
  140. padding: widget.padding,
  141. visualDensity: widget.visualDensity,
  142. shape: widget.shape,
  143. clipBehavior: widget.clipBehavior,
  144. focusNode: focusNode,
  145. autofocus: widget.autofocus,
  146. materialTapTargetSize: widget.materialTapTargetSize,
  147. animationDuration: widget.animationDuration,
  148. minWidth: widget.minWidth,
  149. height: widget.height,
  150. enableFeedback: widget.enableFeedback,
  151. child: widget.child,
  152. );
  153. if (widget.margin != null && widget.margin != EdgeInsets.zero) {
  154. current = Padding(
  155. padding: widget.margin!,
  156. child: current,
  157. );
  158. }
  159. // if (widget.boxShadow) {
  160. // }
  161. return current;
  162. }
  163. }
  164. ///传入future 手势触发展示loading图标
  165. class WLoadingButton extends WLoadingWrapper {
  166. WLoadingButton({
  167. Key? key,
  168. Future<void> Function()? onPressed,
  169. ValueChanged<bool>? onHighlightChanged,
  170. ///手势获取焦点
  171. bool? focusOnGesture,
  172. MouseCursor? mouseCursor,
  173. ButtonTextTheme? textTheme,
  174. Color? textColor,
  175. Color? disabledTextColor,
  176. Color? color,
  177. Color? disabledColor,
  178. Color? focusColor,
  179. Color? hoverColor,
  180. Color? highlightColor,
  181. Color? splashColor,
  182. Brightness? colorBrightness,
  183. double elevation = 0,
  184. double focusElevation = 0,
  185. double hoverElevation = 0,
  186. double highlightElevation = 0,
  187. double disabledElevation = 0,
  188. EdgeInsetsGeometry? padding,
  189. EdgeInsetsGeometry? margin,
  190. VisualDensity? visualDensity,
  191. ShapeBorder? shape,
  192. Clip clipBehavior = Clip.none,
  193. FocusNode? focusNode,
  194. bool autofocus = false,
  195. MaterialTapTargetSize? materialTapTargetSize,
  196. Duration? animationDuration,
  197. double? minWidth,
  198. double? height,
  199. bool enableFeedback = true,
  200. Widget? child,
  201. bool remain = false,
  202. double? loadingSize,
  203. double? loadingStrokeWidth,
  204. Widget? loadingText,
  205. }) : super(
  206. key: key,
  207. remain: remain,
  208. action: onPressed,
  209. color: color,
  210. strokeWidth: loadingStrokeWidth,
  211. text: loadingText,
  212. size: loadingSize,
  213. child: child,
  214. builder: (context, action, child) {
  215. return WButton(
  216. onPressed: action ?? () => {},
  217. onHighlightChanged: onHighlightChanged,
  218. mouseCursor: mouseCursor,
  219. textTheme: textTheme,
  220. textColor: textColor,
  221. disabledTextColor: disabledTextColor,
  222. color: color,
  223. disabledColor: disabledColor,
  224. focusColor: focusColor,
  225. hoverColor: hoverColor,
  226. highlightColor: highlightColor,
  227. splashColor: splashColor,
  228. colorBrightness: colorBrightness,
  229. elevation: elevation,
  230. focusElevation: focusElevation,
  231. hoverElevation: hoverElevation,
  232. highlightElevation: highlightElevation,
  233. disabledElevation: disabledElevation,
  234. padding: padding,
  235. margin: margin,
  236. visualDensity: visualDensity,
  237. shape: shape,
  238. clipBehavior: clipBehavior,
  239. focusNode: focusNode,
  240. autofocus: autofocus,
  241. materialTapTargetSize: materialTapTargetSize,
  242. animationDuration: animationDuration,
  243. minWidth: minWidth,
  244. height: height,
  245. enableFeedback: enableFeedback,
  246. focusOnGesture: focusOnGesture,
  247. child: child,
  248. );
  249. },
  250. );
  251. }