app_button.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import 'package:flutter/material.dart';
  2. import '../../theme/app_colors.dart';
  3. import '../../utils/constants.dart';
  4. /// 按钮类型
  5. enum ButtonType { primary, secondary, danger, text }
  6. /// 按钮尺寸
  7. enum ButtonSize { small, medium, large }
  8. /// 应用按钮组件
  9. class AppButton extends StatelessWidget {
  10. final String? text;
  11. final Widget? child;
  12. final VoidCallback? onPressed;
  13. final ButtonType type;
  14. final ButtonSize size;
  15. final bool loading;
  16. final IconData? icon;
  17. final bool fullWidth;
  18. const AppButton({
  19. Key? key,
  20. this.text,
  21. this.child,
  22. this.onPressed,
  23. this.type = ButtonType.primary,
  24. this.size = ButtonSize.medium,
  25. this.loading = false,
  26. this.icon,
  27. this.fullWidth = false,
  28. }) : assert(text != null || child != null, 'text or child must be provided'),
  29. super(key: key);
  30. @override
  31. Widget build(BuildContext context) {
  32. final content = child ?? Text(text!);
  33. final buttonContent = loading
  34. ? SizedBox(
  35. width: _getIconSize(),
  36. height: _getIconSize(),
  37. child: CircularProgressIndicator(
  38. strokeWidth: 2,
  39. valueColor: AlwaysStoppedAnimation<Color>(_getTextColor()),
  40. ),
  41. )
  42. : Row(
  43. mainAxisSize: fullWidth ? MainAxisSize.max : MainAxisSize.min,
  44. mainAxisAlignment: MainAxisAlignment.center,
  45. children: [
  46. if (icon != null) ...[
  47. Icon(icon, size: _getIconSize(), color: _getTextColor()),
  48. SizedBox(width: AppSpacing.sm),
  49. ],
  50. content,
  51. ],
  52. );
  53. final button = _buildButton(context, buttonContent);
  54. if (fullWidth) {
  55. return SizedBox(width: double.infinity, child: button);
  56. }
  57. return button;
  58. }
  59. Widget _buildButton(BuildContext context, Widget content) {
  60. switch (type) {
  61. case ButtonType.primary:
  62. return ElevatedButton(
  63. onPressed: loading ? null : onPressed,
  64. style: ElevatedButton.styleFrom(
  65. backgroundColor: AppColors.primary,
  66. foregroundColor: Colors.white,
  67. padding: _getPadding(),
  68. minimumSize: Size(0, _getHeight()),
  69. shape: RoundedRectangleBorder(
  70. borderRadius: BorderRadius.circular(6),
  71. ),
  72. ),
  73. child: content,
  74. );
  75. case ButtonType.secondary:
  76. return OutlinedButton(
  77. onPressed: loading ? null : onPressed,
  78. style: OutlinedButton.styleFrom(
  79. foregroundColor: AppColors.primary,
  80. padding: _getPadding(),
  81. minimumSize: Size(0, _getHeight()),
  82. side: const BorderSide(color: AppColors.primary),
  83. shape: RoundedRectangleBorder(
  84. borderRadius: BorderRadius.circular(6),
  85. ),
  86. ),
  87. child: content,
  88. );
  89. case ButtonType.danger:
  90. return ElevatedButton(
  91. onPressed: loading ? null : onPressed,
  92. style: ElevatedButton.styleFrom(
  93. backgroundColor: AppColors.error,
  94. foregroundColor: Colors.white,
  95. padding: _getPadding(),
  96. minimumSize: Size(0, _getHeight()),
  97. shape: RoundedRectangleBorder(
  98. borderRadius: BorderRadius.circular(6),
  99. ),
  100. ),
  101. child: content,
  102. );
  103. case ButtonType.text:
  104. return TextButton(
  105. onPressed: loading ? null : onPressed,
  106. style: TextButton.styleFrom(
  107. foregroundColor: AppColors.primary,
  108. padding: _getPadding(),
  109. minimumSize: Size(0, _getHeight()),
  110. shape: RoundedRectangleBorder(
  111. borderRadius: BorderRadius.circular(6),
  112. ),
  113. ),
  114. child: content,
  115. );
  116. }
  117. }
  118. EdgeInsets _getPadding() {
  119. switch (size) {
  120. case ButtonSize.small:
  121. return const EdgeInsets.symmetric(horizontal: 12, vertical: 6);
  122. case ButtonSize.medium:
  123. return const EdgeInsets.symmetric(horizontal: 24, vertical: 12);
  124. case ButtonSize.large:
  125. return const EdgeInsets.symmetric(horizontal: 32, vertical: 16);
  126. }
  127. }
  128. double _getHeight() {
  129. switch (size) {
  130. case ButtonSize.small:
  131. return 28;
  132. case ButtonSize.medium:
  133. return 40;
  134. case ButtonSize.large:
  135. return 48;
  136. }
  137. }
  138. double _getIconSize() {
  139. switch (size) {
  140. case ButtonSize.small:
  141. return 14;
  142. case ButtonSize.medium:
  143. return 18;
  144. case ButtonSize.large:
  145. return 20;
  146. }
  147. }
  148. Color _getTextColor() {
  149. switch (type) {
  150. case ButtonType.primary:
  151. case ButtonType.danger:
  152. return Colors.white;
  153. case ButtonType.secondary:
  154. case ButtonType.text:
  155. return AppColors.primary;
  156. }
  157. }
  158. }