button.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import 'package:auto_size_text/auto_size_text.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import '../../common.dart';
  5. class Button extends StatefulWidget {
  6. final GestureTapCallback onTap;
  7. final String text;
  8. final double? textSize;
  9. final double? minWidth;
  10. final bool isOutline;
  11. final double? padding;
  12. final Color? textColor;
  13. final double? radius;
  14. final Color? borderColor;
  15. Button({
  16. Key? key,
  17. this.minWidth,
  18. this.isOutline = false,
  19. this.textSize,
  20. this.padding,
  21. this.textColor,
  22. this.radius,
  23. this.borderColor,
  24. required this.onTap,
  25. required this.text,
  26. }) : super(key: key);
  27. @override
  28. State<Button> createState() => _ButtonState();
  29. }
  30. class _ButtonState extends State<Button> {
  31. RxBool hover = false.obs;
  32. RxBool pressed = false.obs;
  33. @override
  34. Widget build(BuildContext context) {
  35. return Obx(() => InkWell(
  36. onTapDown: (_) => pressed.value = true,
  37. onTapUp: (_) => pressed.value = false,
  38. onTapCancel: () => pressed.value = false,
  39. onHover: (value) => hover.value = value,
  40. onTap: widget.onTap,
  41. child: ConstrainedBox(
  42. constraints: BoxConstraints(
  43. minWidth: widget.minWidth ?? 70.0,
  44. ),
  45. child: Container(
  46. padding: EdgeInsets.all(widget.padding ?? 4.5),
  47. alignment: Alignment.center,
  48. decoration: BoxDecoration(
  49. color: pressed.value
  50. ? MyTheme.accent
  51. : (widget.isOutline
  52. ? Colors.transparent
  53. : MyTheme.button),
  54. border: Border.all(
  55. color: pressed.value
  56. ? MyTheme.accent
  57. : hover.value
  58. ? MyTheme.hoverBorder
  59. : (widget.isOutline
  60. ? widget.borderColor ?? MyTheme.border
  61. : MyTheme.button),
  62. ),
  63. borderRadius: BorderRadius.circular(widget.radius ?? 5),
  64. ),
  65. child: Text(
  66. translate(
  67. widget.text,
  68. ),
  69. style: TextStyle(
  70. fontSize: widget.textSize ?? 12.0,
  71. color: widget.isOutline
  72. ? widget.textColor ??
  73. Theme.of(context).textTheme.titleLarge?.color
  74. : Colors.white),
  75. ).marginSymmetric(horizontal: 12),
  76. )),
  77. ));
  78. }
  79. }
  80. class FixedWidthButton extends StatefulWidget {
  81. final GestureTapCallback onTap;
  82. final String text;
  83. final double? textSize;
  84. final double width;
  85. final bool isOutline;
  86. final double? padding;
  87. final Color? textColor;
  88. final double? radius;
  89. final Color? borderColor;
  90. final int? maxLines;
  91. FixedWidthButton({
  92. Key? key,
  93. required this.width,
  94. this.maxLines,
  95. this.isOutline = false,
  96. this.textSize,
  97. this.padding,
  98. this.textColor,
  99. this.radius,
  100. this.borderColor,
  101. required this.onTap,
  102. required this.text,
  103. }) : super(key: key);
  104. @override
  105. State<FixedWidthButton> createState() => _FixedWidthButtonState();
  106. }
  107. class _FixedWidthButtonState extends State<FixedWidthButton> {
  108. RxBool hover = false.obs;
  109. RxBool pressed = false.obs;
  110. @override
  111. Widget build(BuildContext context) {
  112. return Obx(() => InkWell(
  113. onTapDown: (_) => pressed.value = true,
  114. onTapUp: (_) => pressed.value = false,
  115. onTapCancel: () => pressed.value = false,
  116. onHover: (value) => hover.value = value,
  117. onTap: widget.onTap,
  118. child: Container(
  119. width: widget.width,
  120. padding: EdgeInsets.all(widget.padding ?? 4.5),
  121. alignment: Alignment.center,
  122. decoration: BoxDecoration(
  123. color: pressed.value
  124. ? MyTheme.accent
  125. : (widget.isOutline ? Colors.transparent : MyTheme.button),
  126. border: Border.all(
  127. color: pressed.value
  128. ? MyTheme.accent
  129. : hover.value
  130. ? MyTheme.hoverBorder
  131. : (widget.isOutline
  132. ? widget.borderColor ?? MyTheme.border
  133. : MyTheme.button),
  134. ),
  135. borderRadius: BorderRadius.circular(widget.radius ?? 5),
  136. ),
  137. child: Row(
  138. mainAxisAlignment: MainAxisAlignment.center,
  139. children: [
  140. Flexible(
  141. child: AutoSizeText(
  142. translate(
  143. widget.text,
  144. ),
  145. maxLines: widget.maxLines ?? 1,
  146. textAlign: TextAlign.center,
  147. style: TextStyle(
  148. fontSize: widget.textSize ?? 12.0,
  149. color: widget.isOutline
  150. ? widget.textColor ??
  151. Theme.of(context).textTheme.titleLarge?.color
  152. : Colors.white),
  153. ).marginSymmetric(horizontal: 12),
  154. ),
  155. ],
  156. ),
  157. ),
  158. ));
  159. }
  160. }