-
-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathactions.dart
More file actions
236 lines (203 loc) · 6.17 KB
/
actions.dart
File metadata and controls
236 lines (203 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import 'package:flutter/material.dart';
import 'slidable.dart';
/// Signature for [CustomSlidableAction.onPressed].
typedef SlidableActionCallback = void Function(BuildContext context);
const int _kFlex = 1;
const Color _kBackgroundColor = Colors.white;
const bool _kAutoClose = true;
/// Represents an action of an [ActionPane].
class CustomSlidableAction extends StatelessWidget {
/// Creates a [CustomSlidableAction].
///
/// The [flex], [backgroundColor], [autoClose] and [child] arguments must not
/// be null.
///
/// The [flex] argument must also be greater than 0.
const CustomSlidableAction({
super.key,
this.flex = _kFlex,
this.backgroundColor = _kBackgroundColor,
this.foregroundColor,
this.autoClose = _kAutoClose,
this.borderRadius = BorderRadiusDirectional.zero,
this.padding,
required this.onPressed,
required this.child,
}) : assert(flex > 0);
/// {@template slidable.actions.flex}
/// The flex factor to use for this child.
///
/// The amount of space the child's can occupy in the main axis is
/// determined by dividing the free space according to the flex factors of the
/// other [CustomSlidableAction]s.
/// {@endtemplate}
final int flex;
/// {@template slidable.actions.backgroundColor}
/// The background color of this action.
///
/// Defaults to [Colors.white].
/// {@endtemplate}
final Color backgroundColor;
/// {@template slidable.actions.foregroundColor}
/// The foreground color of this action.
///
/// Defaults to [Colors.black] if [background]'s brightness is
/// [Brightness.light], or to [Colors.white] if [background]'s brightness is
/// [Brightness.dark].
/// {@endtemplate}
final Color? foregroundColor;
/// {@template slidable.actions.autoClose}
/// Whether the enclosing [Slidable] will be closed after [onPressed]
/// occurred.
/// {@endtemplate}
final bool autoClose;
/// {@template slidable.actions.onPressed}
/// Called when the action is tapped or otherwise activated.
///
/// If this callback is null, then the action will be disabled.
/// {@endtemplate}
final SlidableActionCallback? onPressed;
/// {@template slidable.actions.borderRadius}
/// The borderRadius of this action
///
/// Defaults to [BorderRadius.zero].
/// {@endtemplate}
final BorderRadiusDirectional borderRadius;
/// {@template slidable.actions.padding}
/// The padding of the OutlinedButton
/// {@endtemplate}
final EdgeInsets? padding;
/// Typically the action's icon or label.
final Widget child;
@override
Widget build(BuildContext context) {
final effectiveForegroundColor = foregroundColor ??
(ThemeData.estimateBrightnessForColor(backgroundColor) ==
Brightness.light
? Colors.black
: Colors.white);
return Expanded(
flex: flex,
child: SizedBox.expand(
child: OutlinedButton(
onPressed: () => _handleTap(context),
style: OutlinedButton.styleFrom(
padding: padding,
backgroundColor: backgroundColor,
disabledForegroundColor: effectiveForegroundColor.withValues(
alpha: 0.38,
),
iconColor: effectiveForegroundColor,
foregroundColor: effectiveForegroundColor,
shape: RoundedRectangleBorder(
borderRadius: borderRadius,
),
side: BorderSide.none,
),
child: child,
),
),
);
}
void _handleTap(BuildContext context) {
onPressed?.call(context);
if (autoClose) {
Slidable.of(context)?.close();
}
}
}
/// An action for [Slidable] which can show an icon, a label, or both.
class SlidableAction extends StatelessWidget {
/// Creates a [SlidableAction].
///
/// The [flex], [backgroundColor], [autoClose] and [spacing] arguments
/// must not be null.
///
/// You must set either an [icon] or a [label].
///
/// The [flex] argument must also be greater than 0.
const SlidableAction({
super.key,
this.flex = _kFlex,
this.backgroundColor = _kBackgroundColor,
this.foregroundColor,
this.autoClose = _kAutoClose,
required this.onPressed,
this.icon,
this.spacing = 4,
this.label,
this.style,
this.borderRadius = BorderRadiusDirectional.zero,
this.padding,
}) : assert(flex > 0),
assert(icon != null || label != null);
/// {@macro slidable.actions.flex}
final int flex;
/// {@macro slidable.actions.backgroundColor}
final Color backgroundColor;
/// {@macro slidable.actions.foregroundColor}
final Color? foregroundColor;
/// {@macro slidable.actions.autoClose}
final bool autoClose;
/// {@macro slidable.actions.onPressed}
final SlidableActionCallback? onPressed;
/// An icon to display above the [label].
final IconData? icon;
/// The space between [icon] and [label] if both set.
///
/// Defaults to 4.
final double spacing;
/// A label to display below the [icon].
final String? label;
/// A style for the label [label].
final TextStyle? style;
/// Padding of the OutlinedButton
final BorderRadiusDirectional borderRadius;
/// Padding of the OutlinedButton
final EdgeInsets? padding;
@override
Widget build(BuildContext context) {
final children = <Widget>[];
if (icon != null) {
children.add(
Icon(icon),
);
}
if (label != null) {
if (children.isNotEmpty) {
children.add(
SizedBox(height: spacing),
);
}
children.add(
Text(
label!,
overflow: TextOverflow.ellipsis,
style: style,
),
);
}
final child = children.length == 1
? children.first
: Column(
mainAxisSize: MainAxisSize.min,
children: [
...children.map(
(child) => Flexible(
child: child,
),
)
],
);
return CustomSlidableAction(
borderRadius: borderRadius,
padding: padding,
onPressed: onPressed,
autoClose: autoClose,
backgroundColor: backgroundColor,
foregroundColor: foregroundColor,
flex: flex,
child: child,
);
}
}