Line data Source code
1 : import 'package:flutter/material.dart';
2 :
3 : /// BASF button types
4 1 : enum ButtonType {
5 : /// BASF text button type
6 : text,
7 :
8 : /// BASF outlined button type
9 : outlined,
10 : }
11 :
12 : /// {@template basf_button}
13 : /// Basf styled buttons button
14 : /// {@endtemplate}
15 : abstract class BasfButton extends StatefulWidget {
16 : /// {@macro basf_button}
17 1 : const BasfButton({
18 : super.key,
19 : this.text,
20 : this.leadingIcon,
21 : this.trailingIcon,
22 : this.iconSize,
23 : this.child,
24 : this.onPressed,
25 : this.onLongPress,
26 : this.style,
27 : this.size,
28 : this.expanded = false,
29 : this.alignment = Alignment.center,
30 : });
31 :
32 : /// Use this to show text on the button directly.
33 : final String? text;
34 :
35 : /// Icon on the left
36 : final IconData? leadingIcon;
37 :
38 : /// Icon on the right˜
39 : final IconData? trailingIcon;
40 :
41 : /// Size of default icons
42 : final double? iconSize;
43 :
44 : /// Overrides all widgets (For example to show loader)
45 : final Widget? child;
46 :
47 : /// If this function is not set, the appearance of the button might be bad.
48 : final VoidCallback? onPressed;
49 :
50 : /// If this function is not set, the appearance of the button might be bad.
51 : final VoidCallback? onLongPress;
52 :
53 : /// Custom style different from the theme
54 : final ButtonStyle? style;
55 :
56 : /// The minimum width of the button.
57 : final Size? size;
58 :
59 : /// Maximizes button size
60 : final bool expanded;
61 :
62 : /// Button alignment
63 : final AlignmentGeometry? alignment;
64 :
65 : /// Button content
66 1 : Widget buttonStandardContent() {
67 1 : return Row(
68 : mainAxisAlignment: MainAxisAlignment.center,
69 1 : mainAxisSize: expanded ? MainAxisSize.max : MainAxisSize.min,
70 1 : children: [
71 3 : if (leadingIcon != null) icon(leadingIcon!),
72 3 : if (leadingIcon != null && text != null) const SizedBox(width: 12),
73 2 : if (text != null) buttonText(),
74 3 : if (trailingIcon != null && text != null) const SizedBox(width: 12),
75 3 : if (trailingIcon != null) icon(trailingIcon!),
76 : ],
77 : );
78 : }
79 :
80 : /// Button child content
81 1 : Widget buttonChildContent() {
82 1 : if (expanded) {
83 1 : return SizedBox(
84 : width: double.infinity,
85 2 : child: Align(child: child),
86 : );
87 : } else {
88 1 : return child!;
89 : }
90 : }
91 :
92 : /// Button text
93 1 : Widget buttonText() {
94 1 : return Flexible(
95 1 : child: Text(
96 1 : text!,
97 : maxLines: 1,
98 : overflow: TextOverflow.ellipsis,
99 : textAlign: TextAlign.center,
100 : ),
101 : );
102 : }
103 :
104 : /// Icon
105 1 : Widget icon(IconData iconData) {
106 1 : return Center(
107 2 : child: Icon(iconData, size: iconSize ?? 19),
108 : );
109 : }
110 :
111 : /// Style adjustments
112 1 : ButtonStyle getStyleWithAdjustments({
113 : required BuildContext context,
114 : required ButtonType buttonType,
115 : ButtonStyle? style,
116 : }) {
117 1 : ButtonStyle getButtonStyle() {
118 : switch (buttonType) {
119 1 : case ButtonType.text:
120 3 : return Theme.of(context).textButtonTheme.style!;
121 1 : case ButtonType.outlined:
122 3 : return Theme.of(context).outlinedButtonTheme.style!;
123 : }
124 : }
125 :
126 2 : final buttonStyle = style ?? this.style ?? getButtonStyle();
127 : final buttonSize =
128 1 : size == null ? null : MaterialStateProperty.all<Size>(size!);
129 :
130 1 : return buttonStyle.copyWith(
131 1 : maximumSize: buttonSize ?? buttonStyle.maximumSize,
132 1 : minimumSize: buttonSize ?? buttonStyle.minimumSize,
133 : );
134 : }
135 : }
|