Repository navigation
Expand file tree
/
Copy pathpadding_effect.dart
More file actions
301 lines (276 loc) · 10.1 KB
/
Copy pathpadding_effect.dart
File metadata and controls
301 lines (276 loc) · 10.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import 'package:flutter/widgets.dart';
import '../effect_widget.dart';
import 'effect.dart';
import 'vector_effect.dart';
/// Provides a extension method to apply an [PaddingEffect] to a [Widget].
extension PaddingEffectExt on Widget {
/// Applies an [PaddingEffect] to a [Widget] with the given [padding].
Widget pad(
EdgeInsets padding, {
EdgeInsets? from,
}) {
return EffectWidget(
start: from == null ? null : PaddingEffect(padding: from),
end: PaddingEffect(padding: padding),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [padding].
Widget padAll(
double padding, {
double? from,
}) {
return EffectWidget(
start: from == null ? null : PaddingEffect(padding: EdgeInsets.all(from)),
end: PaddingEffect(padding: EdgeInsets.all(padding)),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [vertical]
/// and [horizontal] padding.
Widget padSymmetric(
{double vertical = 0, double horizontal = 0, double? from}) {
return EffectWidget(
start: from == null
? null
: PaddingEffect(
padding: EdgeInsets.symmetric(vertical: from, horizontal: from)),
end: PaddingEffect(
padding:
EdgeInsets.symmetric(vertical: vertical, horizontal: horizontal)),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [horizontal]
/// padding.
Widget padHorizontal(
double padding, {
double? from,
}) {
return EffectWidget(
start: from == null
? null
: PaddingEffect(padding: EdgeInsets.symmetric(horizontal: from)),
end: PaddingEffect(padding: EdgeInsets.symmetric(horizontal: padding)),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [vertical]
/// padding.
Widget padVertical(
double padding, {
double? from,
}) {
return EffectWidget(
start: from == null
? null
: PaddingEffect(padding: EdgeInsets.symmetric(vertical: from)),
end: PaddingEffect(padding: EdgeInsets.symmetric(vertical: padding)),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [left], [right],
/// [top], and [bottom] padding.
Widget padOnly({
double left = 0,
double right = 0,
double top = 0,
double bottom = 0,
double? from,
}) {
return EffectWidget(
start: from == null
? null
: PaddingEffect(
padding: EdgeInsets.only(
left: from, right: from, top: from, bottom: from)),
end: PaddingEffect(
padding: EdgeInsets.only(
left: left, right: right, top: top, bottom: bottom)),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [left] padding.
Widget padLeft(
double padding, {
double? from,
}) {
return EffectWidget(
start: from == null
? null
: PaddingEffect(padding: EdgeInsets.only(left: from)),
end: PaddingEffect(padding: EdgeInsets.only(left: padding)),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [right] padding.
Widget padRight(
double padding, {
double? from,
}) {
return EffectWidget(
start: from == null
? null
: PaddingEffect(padding: EdgeInsets.only(right: from)),
end: PaddingEffect(padding: EdgeInsets.only(right: padding)),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [top] padding.
Widget padTop(
double padding, {
double? from,
}) {
return EffectWidget(
start: from == null
? null
: PaddingEffect(padding: EdgeInsets.only(top: from)),
end: PaddingEffect(padding: EdgeInsets.only(top: padding)),
child: this,
);
}
/// Applies an [PaddingEffect] to a [Widget] with the given [bottom] padding.
Widget padBottom(
double padding, {
double? from,
}) {
return EffectWidget(
start: from == null
? null
: PaddingEffect(padding: EdgeInsets.only(bottom: from)),
end: PaddingEffect(padding: EdgeInsets.only(bottom: padding)),
child: this,
);
}
}
/// An effect that insets a [Widget] by a given [padding].
///
/// [PaddingEffect] is a [VectorEffect]: its four insets add, subtract and
/// scale as a single vector quantity, which is what lets padding take part
/// in the same physics as every other animatable geometry in this library.
/// Two things follow from that, and neither is available to effects that
/// stay on the normalized lerp path:
///
/// * **Overshoot is real.** A spring does not creep up on its target and
/// stop; it sails past and comes back. Under a bouncy motion `.pad()`
/// now describes the same curve as a sibling `.translateY()` driven by
/// the same motion, instead of quietly arriving early and flat. Before
/// this, mixing the two on one widget meant either accepting that the
/// geometry disagreed with itself or moving everything off padding to
/// keep it consistent.
/// * **Momentum survives a retarget.** Changing the target mid-flight
/// hands the in-flight velocity to the new spring instead of restarting
/// it from rest, so rapid re-triggers whip rather than hitch.
///
/// The price of real overshoot is that the interpolated value is no longer
/// bounded by its two endpoints: an inset animating down toward zero passes
/// through negative numbers on the way back up. [RenderPadding] asserts
/// `padding.isNonNegative`, so a negative inset can never reach a [Padding]
/// widget.
///
/// That guard lives in [apply], not in [lerp] — the same division of labour
/// `OpacityEffect` uses. Clamping inside [lerp] protects nothing that
/// [apply] cannot protect, and pays for it by flattening the curve for
/// every consumer, including the ones whose padding never goes anywhere
/// near zero. Clamping inside [apply] floors only the components that
/// actually went negative, at the last possible moment, and leaves the
/// algebra exact.
class PaddingEffect extends Effect with VectorEffect<PaddingEffect> {
/// The insets applied to the [Widget].
///
/// Mid-flight this may hold values outside the animation's endpoints,
/// including negative ones — see the class documentation. [apply] is
/// where it is made safe for layout.
final EdgeInsets padding;
/// Creates a [PaddingEffect] with the given [padding].
PaddingEffect({
this.padding = EdgeInsets.zero,
}) : assert(!padding.left.isNaN, 'padding.left must not be NaN'),
assert(!padding.top.isNaN, 'padding.top must not be NaN'),
assert(!padding.right.isNaN, 'padding.right must not be NaN'),
assert(!padding.bottom.isNaN, 'padding.bottom must not be NaN');
/// Interpolates toward [other], unclamped in both the parameter and the
/// result.
///
/// [value] below 0 or above 1 extrapolates rather than saturating: that
/// is exactly how an overshooting curve (or a spring evaluated through
/// this path) expresses its overshoot. Non-negativity is restored in
/// [apply].
@override
PaddingEffect lerp(covariant PaddingEffect other, double value) {
return PaddingEffect(
padding:
EdgeInsets.lerp(padding, other.padding, value) ?? EdgeInsets.zero,
);
}
/// Builds the [Padding], flooring any inset that overshot below zero.
///
/// Only the negative components are touched; an inset that overshot
/// upward is passed through at full size, because a larger-than-target
/// padding is perfectly representable and is the visible half of the
/// bounce. This is the single point where the [RenderPadding] assert
/// applies, because it is the single point where the value becomes
/// layout.
@override
Widget apply(BuildContext context, Widget? child) {
return Padding(
padding: padding.isNonNegative
? padding
: EdgeInsets.only(
left: _floor(padding.left),
top: _floor(padding.top),
right: _floor(padding.right),
bottom: _floor(padding.bottom),
),
child: child,
);
}
static double _floor(double value) => value < 0 ? 0 : value;
@override
PaddingEffect idle() => PaddingEffect();
/// Field-wise sum of the four insets.
///
/// [PaddingEffect] carries no configuration, so there is nothing to ride
/// along from the left operand — every value it holds is animatable.
@override
PaddingEffect operator +(PaddingEffect other) =>
PaddingEffect(padding: padding + other.padding);
/// Field-wise difference of the four insets. Routinely negative: this is
/// the displacement term of the spring solution, not a renderable state.
@override
PaddingEffect operator -(PaddingEffect other) => PaddingEffect(
padding: EdgeInsets.fromLTRB(
_subtractInset(padding.left, other.padding.left),
_subtractInset(padding.top, other.padding.top),
_subtractInset(padding.right, other.padding.right),
_subtractInset(padding.bottom, other.padding.bottom),
),
);
/// Insets are non-nullable, so a lane [VectorEffect.subtractLane] poisons
/// (a non-finite pair with no usable displacement) collapses to zero.
static double _subtractInset(double a, double b) =>
VectorEffect.subtractLane(a, b) ?? 0;
/// Scales all four insets by [factor].
@override
PaddingEffect operator *(double factor) =>
PaddingEffect(padding: padding * factor);
/// The squared Euclidean length of the inset 4-vector, used for settle
/// detection.
@override
double get magnitudeSquared =>
padding.left * padding.left +
padding.top * padding.top +
padding.right * padding.right +
padding.bottom * padding.bottom;
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is PaddingEffect &&
other.runtimeType == runtimeType &&
other.padding == padding;
}
@override
int get hashCode => padding.hashCode;
@override
String toString() => 'PaddingEffect(padding: $padding)';
}