Line data Source code
1 : import 'package:basf_flutter_components/basf_flutter_components.dart';
2 : import 'package:flutter/material.dart';
3 :
4 : /// {@template basf_text_button}
5 : /// A BASF-style text button
6 : /// {@endtemplate}
7 :
8 1 : enum TextButtonConstructorType {
9 : /// contained constructor
10 : contained,
11 :
12 : /// transparent constructor
13 : transparent,
14 :
15 : /// hint constructor
16 : hint,
17 : }
18 :
19 : /// {@macro basf_text_button}
20 : class BasfTextButton extends BasfButton {
21 : /// {@macro basf_text_button}
22 : /// contained
23 1 : BasfTextButton.contained({
24 : super.key,
25 : super.text,
26 : super.leadingIcon,
27 : super.trailingIcon,
28 : super.iconSize,
29 : super.child,
30 : super.onPressed,
31 : super.onLongPress,
32 : super.style,
33 : super.size,
34 : super.expanded,
35 : }) {
36 1 : constructorType = TextButtonConstructorType.contained;
37 : }
38 :
39 : /// {@macro basf_text_button}
40 : /// transparent
41 1 : BasfTextButton.transparent({
42 : super.key,
43 : super.text,
44 : super.leadingIcon,
45 : super.trailingIcon,
46 : super.iconSize,
47 : super.child,
48 : super.onPressed,
49 : super.onLongPress,
50 : super.style,
51 : super.size,
52 : super.expanded,
53 : }) {
54 1 : constructorType = TextButtonConstructorType.transparent;
55 : }
56 :
57 : /// {@macro basf_text_button}
58 : /// hint
59 1 : BasfTextButton.hint({
60 : super.key,
61 : super.text,
62 : super.child,
63 : super.onPressed,
64 : super.onLongPress,
65 : super.style,
66 : super.size,
67 : super.expanded,
68 : super.alignment,
69 : }) {
70 1 : constructorType = TextButtonConstructorType.hint;
71 : }
72 :
73 : /// shows with which constructor the widget has been built
74 : late final TextButtonConstructorType constructorType;
75 :
76 1 : @override
77 1 : State<BasfTextButton> createState() => _BasfTextButtonState();
78 : }
79 :
80 : class _BasfTextButtonState extends State<BasfTextButton> with TextButtonHelper {
81 : late final ButtonStyle? _buttonStyle;
82 : bool _styleSet = false;
83 :
84 1 : @override
85 : void didChangeDependencies() {
86 1 : super.didChangeDependencies();
87 :
88 1 : if (!_styleSet) {
89 2 : _buttonStyle = getTextButtonStyle(
90 1 : context: context,
91 2 : constructorType: widget.constructorType,
92 2 : style: widget.style,
93 : );
94 1 : _styleSet = true;
95 : }
96 : }
97 :
98 1 : @override
99 : Widget build(BuildContext context) {
100 1 : return Align(
101 2 : alignment: widget.alignment!,
102 1 : child: _button(context),
103 : );
104 : }
105 :
106 1 : Widget _button(BuildContext context) {
107 1 : return TextButton(
108 2 : onPressed: widget.onPressed,
109 2 : onLongPress: widget.onLongPress,
110 2 : style: widget.getStyleWithAdjustments(
111 : context: context,
112 : buttonType: ButtonType.text,
113 1 : style: _buttonStyle,
114 : ),
115 2 : child: widget.child != null
116 2 : ? widget.buttonChildContent()
117 2 : : widget.buttonStandardContent(),
118 : );
119 : }
120 : }
121 :
122 : /// mixing for text button
123 : mixin TextButtonHelper {
124 : /// TextButton style
125 1 : ButtonStyle? getTextButtonStyle({
126 : required TextButtonConstructorType constructorType,
127 : required BuildContext context,
128 : required ButtonStyle? style,
129 : }) {
130 : switch (constructorType) {
131 1 : case TextButtonConstructorType.contained:
132 : return style;
133 1 : case TextButtonConstructorType.transparent:
134 1 : return getTransparentStyle(
135 : context: context,
136 : style: style,
137 : );
138 1 : case TextButtonConstructorType.hint:
139 1 : return getHintStyle(
140 : context: context,
141 : style: style,
142 : );
143 : }
144 : }
145 :
146 : /// get transparent style
147 1 : ButtonStyle? getTransparentStyle({
148 : required BuildContext context,
149 : required ButtonStyle? style,
150 : }) {
151 : if (style == null) {
152 1 : return ButtonStyles.transparentTextButtonStyle(
153 2 : Theme.of(context).primaryColor,
154 : );
155 : } else {
156 1 : return style.merge(
157 1 : ButtonStyles.transparentTextButtonStyle(
158 2 : Theme.of(context).primaryColor,
159 : ),
160 : );
161 : }
162 : }
163 :
164 : /// get hint style
165 1 : ButtonStyle? getHintStyle({
166 : required BuildContext context,
167 : required ButtonStyle? style,
168 : }) {
169 : if (style == null) {
170 3 : return ButtonStyles.hintTextButtonStyle(Theme.of(context).primaryColor);
171 : } else {
172 1 : return style.merge(
173 1 : ButtonStyles.hintTextButtonStyle(
174 2 : Theme.of(context).primaryColor,
175 : ),
176 : );
177 : }
178 : }
179 : }
|