button.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import 'package:flowy_infra/size.dart';
  2. import 'package:flowy_infra/theme.dart';
  3. import 'package:flowy_infra_ui/style_widget/hover.dart';
  4. import 'package:flowy_infra_ui/style_widget/text.dart';
  5. import 'package:flowy_infra_ui/widget/spacing.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/widgets.dart';
  8. import 'package:provider/provider.dart';
  9. class FlowyButton extends StatelessWidget {
  10. final Widget text;
  11. final VoidCallback? onTap;
  12. final EdgeInsets padding;
  13. final Widget? icon;
  14. final Color hoverColor;
  15. const FlowyButton({
  16. Key? key,
  17. required this.text,
  18. this.onTap,
  19. this.padding = const EdgeInsets.symmetric(horizontal: 3, vertical: 2),
  20. this.icon,
  21. this.hoverColor = Colors.transparent,
  22. }) : super(key: key);
  23. @override
  24. Widget build(BuildContext context) {
  25. return InkWell(
  26. onTap: onTap,
  27. child: FlowyHover(
  28. config: HoverDisplayConfig(borderRadius: Corners.s6Border, hoverColor: hoverColor),
  29. builder: (context, onHover) => _render(),
  30. ),
  31. );
  32. }
  33. Widget _render() {
  34. List<Widget> children = List.empty(growable: true);
  35. if (icon != null) {
  36. children.add(SizedBox.fromSize(size: const Size.square(16), child: icon!));
  37. children.add(const HSpace(6));
  38. }
  39. children.add(Align(child: text));
  40. return Padding(
  41. padding: padding,
  42. child: Row(
  43. children: children,
  44. ),
  45. );
  46. }
  47. }
  48. class FlowyTextButton extends StatelessWidget {
  49. final String text;
  50. final double fontSize;
  51. final TextOverflow overflow;
  52. final FontWeight fontWeight;
  53. final VoidCallback? onPressed;
  54. final EdgeInsets padding;
  55. final Widget? heading;
  56. final Color? hoverColor;
  57. final Color? fillColor;
  58. final BorderRadius? radius;
  59. final MainAxisAlignment mainAxisAlignment;
  60. final String? tooltip;
  61. // final HoverDisplayConfig? hoverDisplay;
  62. const FlowyTextButton(
  63. this.text, {
  64. Key? key,
  65. this.onPressed,
  66. this.fontSize = 16,
  67. this.overflow = TextOverflow.ellipsis,
  68. this.fontWeight = FontWeight.w400,
  69. this.padding = const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
  70. this.hoverColor,
  71. this.fillColor,
  72. this.heading,
  73. this.radius,
  74. this.mainAxisAlignment = MainAxisAlignment.start,
  75. this.tooltip,
  76. }) : super(key: key);
  77. @override
  78. Widget build(BuildContext context) {
  79. List<Widget> children = [];
  80. if (heading != null) {
  81. children.add(heading!);
  82. children.add(const HSpace(6));
  83. }
  84. children.add(
  85. FlowyText(
  86. text,
  87. overflow: overflow,
  88. fontWeight: fontWeight,
  89. fontSize: fontSize,
  90. textAlign: TextAlign.center,
  91. ),
  92. );
  93. Widget child = Padding(
  94. padding: padding,
  95. child: Row(
  96. crossAxisAlignment: CrossAxisAlignment.center,
  97. mainAxisAlignment: mainAxisAlignment,
  98. children: children,
  99. ),
  100. );
  101. child = RawMaterialButton(
  102. visualDensity: VisualDensity.compact,
  103. hoverElevation: 0,
  104. highlightElevation: 0,
  105. shape: RoundedRectangleBorder(borderRadius: radius ?? BorderRadius.circular(2)),
  106. fillColor: fillColor,
  107. hoverColor: hoverColor ?? Colors.transparent,
  108. focusColor: Colors.transparent,
  109. splashColor: Colors.transparent,
  110. highlightColor: Colors.transparent,
  111. elevation: 0,
  112. onPressed: onPressed,
  113. child: child,
  114. );
  115. if (tooltip != null) {
  116. child = Tooltip(
  117. message: tooltip!,
  118. child: child,
  119. );
  120. }
  121. return child;
  122. }
  123. }
  124. // return TextButton(
  125. // style: ButtonStyle(
  126. // textStyle: MaterialStateProperty.all(TextStyle(fontSize: fontSize)),
  127. // alignment: Alignment.centerLeft,
  128. // foregroundColor: MaterialStateProperty.all(Colors.black),
  129. // padding: MaterialStateProperty.all<EdgeInsets>(
  130. // const EdgeInsets.symmetric(horizontal: 2)),
  131. // ),
  132. // onPressed: onPressed,
  133. // child: Text(
  134. // text,
  135. // overflow: TextOverflow.ellipsis,
  136. // softWrap: false,
  137. // ),
  138. // );