main_button.dart 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import 'package:flutter/material.dart';
  2. ///主要按钮
  3. class MainButton extends StatelessWidget {
  4. final void Function() onPressed;
  5. final String text;
  6. final bool isOutlined;
  7. final EdgeInsets? buttonPadding;
  8. final Size? minimumSize;
  9. final Size? maximumSize;
  10. final bool fitMinimumSize;
  11. final OutlinedBorder? shape;
  12. const MainButton(
  13. {super.key,
  14. required this.text,
  15. required this.onPressed,
  16. this.buttonPadding,
  17. this.minimumSize,
  18. this.maximumSize,
  19. this.isOutlined = false,
  20. this.fitMinimumSize = false,
  21. this.shape});
  22. @override
  23. Widget build(BuildContext context) {
  24. return isOutlined
  25. ? OutlinedButton(
  26. onPressed: onPressed,
  27. style: ButtonStyle(
  28. tapTargetSize:
  29. fitMinimumSize ? MaterialTapTargetSize.shrinkWrap : null,
  30. padding: buttonPadding == null
  31. ? null
  32. : WidgetStatePropertyAll(buttonPadding),
  33. minimumSize: minimumSize == null
  34. ? null
  35. : WidgetStatePropertyAll(minimumSize),
  36. maximumSize: maximumSize == null
  37. ? null
  38. : WidgetStatePropertyAll(maximumSize),
  39. side: WidgetStatePropertyAll(
  40. BorderSide(color: Color(0xFFD2D2D2))),
  41. shape: shape == null ? null : WidgetStatePropertyAll(shape)),
  42. child: Text(
  43. text,
  44. style: TextStyle(color: Theme.of(context).colorScheme.onSurface),
  45. ))
  46. : ElevatedButton(
  47. onPressed: onPressed,
  48. style: ButtonStyle(
  49. tapTargetSize:
  50. fitMinimumSize ? MaterialTapTargetSize.shrinkWrap : null,
  51. padding: buttonPadding == null
  52. ? null
  53. : WidgetStatePropertyAll(buttonPadding),
  54. backgroundColor:
  55. WidgetStatePropertyAll(Theme.of(context).primaryColor),
  56. minimumSize: minimumSize == null
  57. ? null
  58. : WidgetStatePropertyAll(minimumSize),
  59. maximumSize: maximumSize == null
  60. ? null
  61. : WidgetStatePropertyAll(maximumSize),
  62. elevation: const WidgetStatePropertyAll(0.0),
  63. shape: shape == null ? null : WidgetStatePropertyAll(shape)),
  64. child: Text(
  65. text,
  66. textAlign: TextAlign.center,
  67. style: TextStyle(
  68. color: Theme.of(context).colorScheme.onPrimary,
  69. fontWeight: FontWeight.bold),
  70. ));
  71. }
  72. }