Repository navigation
Expand file tree
/
Copy pathalign_effect.dart
More file actions
284 lines (262 loc) · 11.2 KB
/
Copy pathalign_effect.dart
File metadata and controls
284 lines (262 loc) · 11.2 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
import 'dart:ui';
import 'package:flutter/widgets.dart';
import '../../hyper_effects.dart';
/// Provides a extension method to apply an [AlignEffect] to a [Widget].
extension AlignEffectExt on Widget {
/// Applies an [AlignEffect] to a [Widget] with the given [alignment],
/// optionally animating from [from].
///
/// [widthFactor] and [heightFactor] mirror [Align]'s parameters: when
/// null (the default), the widget expands to fill its incoming
/// constraints; when set, the widget sizes itself to the child's size
/// multiplied by the factor. [fromWidthFactor] and [fromHeightFactor]
/// provide the starting factors, defaulting to [widthFactor] and
/// [heightFactor]. A null and a non-null factor cannot be interpolated,
/// so such a pair snaps to the target value instead of animating.
Widget align(
AlignmentGeometry alignment, {
AlignmentGeometry? from,
double? heightFactor,
double? widthFactor,
double? fromHeightFactor,
double? fromWidthFactor,
}) {
return EffectWidget(
start: from == null && fromHeightFactor == null && fromWidthFactor == null
? null
: AlignEffect(
alignment: from ?? alignment,
heightFactor: fromHeightFactor ?? heightFactor,
widthFactor: fromWidthFactor ?? widthFactor,
),
end: AlignEffect(
alignment: alignment,
heightFactor: heightFactor,
widthFactor: widthFactor,
),
child: this,
);
}
/// Applies an [AlignEffect] to a [Widget] only on the x-axis.
///
/// The widget expands to fill its incoming constraints, matching the
/// default behavior of [Align].
Widget alignX(double x, {double? from}) {
return EffectWidget(
start: from == null ? null : AlignEffect(alignment: Alignment(from, 0)),
end: AlignEffect(alignment: Alignment(x, 0)),
child: this,
);
}
/// Applies an [AlignEffect] to a [Widget] only on the y-axis.
///
/// The widget expands to fill its incoming constraints, matching the
/// default behavior of [Align].
Widget alignY(double y, {double? from}) {
return EffectWidget(
start: from == null ? null : AlignEffect(alignment: Alignment(0, from)),
end: AlignEffect(alignment: Alignment(0, y)),
child: this,
);
}
/// Applies an [AlignEffect] to a [Widget] with the given [x] and [y]
/// values.
///
/// The widget expands to fill its incoming constraints, matching the
/// default behavior of [Align].
Widget alignXY(
double x,
double y, {
AlignmentGeometry? from,
}) {
return EffectWidget(
start: from == null ? null : AlignEffect(alignment: from),
end: AlignEffect(alignment: Alignment(x, y)),
child: this,
);
}
}
/// An effect that aligns a [Widget] by a given [alignment], optionally
/// sizing it to a factor of its child.
///
/// [AlignEffect] is a [VectorEffect], so an alignment animation gets real
/// spring overshoot and carries its momentum across a mid-flight retarget
/// instead of restarting from rest. The alignment itself vectorizes
/// cleanly: [AlignmentGeometry] already defines addition
/// ([AlignmentGeometry.add]), negation and scaling, and defines them ACROSS
/// the [Alignment] / [AlignmentDirectional] divide, so the two endpoints of
/// an animation are never required to agree on a runtime type.
///
/// ## Why the size factors are the awkward part
///
/// A null [widthFactor] does not mean "zero" — it means "fill the incoming
/// constraints", which is a different layout mode rather than a smaller
/// number. There is no arithmetic between a mode and a number, so
/// nullability is treated as CONFIGURATION and rides along from the LEFT
/// operand, exactly as [VectorEffect] prescribes for fields that are not
/// animatable:
///
/// * both operands carry a factor — the numbers combine, and the factor
/// animates like any other scalar;
/// * neither carries one — the result carries none;
/// * only the left carries one — its value passes through unchanged,
/// because there is nothing numeric on the right to combine it with.
///
/// That asymmetry is deliberate, and it lands on the documented behaviour
/// rather than beside it. The spring solver builds every intermediate state
/// as `target + displacement * coefficient`, with the TARGET as the left
/// operand, so a mixed null / non-null pair resolves to the target's mode
/// on the very first frame — which is precisely the snap-to-target rule
/// [lerp] has always applied, reached by a different route. The two paths
/// agreeing is a behavioural contract, not a coincidence, and
/// `test/align_effect_vector_test.dart` pins it.
///
/// ## Where the clamping lives
///
/// [RenderPositionedBox] asserts `widthFactor == null || widthFactor >= 0`,
/// so a factor that overshoots below zero is floored in [apply], where the
/// value becomes layout and the assert applies. [lerp] keeps its own floor
/// as well: unlike a clamp on the interpolation PARAMETER, flooring the
/// RESULT at zero destroys nothing, because a negative factor is not a
/// value the layout can express in the first place. Overshoot above the
/// target is untouched on both paths.
///
/// [alignment] needs no guard at all. Values outside the -1..1 box are
/// meaningful to [Align] — they simply place the child past the edge — and
/// they are what makes a bouncy `.alignX()` actually bounce.
class AlignEffect extends Effect with VectorEffect<AlignEffect> {
/// The alignment by which the [Widget] is aligned.
///
/// Mid-flight this may hold values outside the animation's endpoints;
/// that overshoot is intentional and is passed straight through to
/// [Align].
final AlignmentGeometry alignment;
/// Sets its width to the child's width multiplied by this factor.
/// If null, the widget expands to fill its incoming width constraints,
/// matching the default behavior of [Align].
///
/// Null and non-null are different layout modes, not different numbers:
/// see the class documentation for how a mixed pair resolves.
final double? widthFactor;
/// Sets its height to the child's height multiplied by this factor.
/// If null, the widget expands to fill its incoming height constraints,
/// matching the default behavior of [Align].
///
/// Null and non-null are different layout modes, not different numbers:
/// see the class documentation for how a mixed pair resolves.
final double? heightFactor;
/// Creates an [AlignEffect] with the given [alignment], [widthFactor],
/// and [heightFactor].
AlignEffect({
this.alignment = AlignmentDirectional.topStart,
this.widthFactor,
this.heightFactor,
}) : assert(widthFactor == null || !widthFactor.isNaN,
'widthFactor must not be NaN'),
assert(heightFactor == null || !heightFactor.isNaN,
'heightFactor must not be NaN');
@override
AlignEffect lerp(covariant AlignEffect other, double value) {
return AlignEffect(
alignment: AlignmentGeometry.lerp(alignment, other.alignment, value) ??
AlignmentDirectional.topStart,
widthFactor: _lerpFactor(widthFactor, other.widthFactor, value),
heightFactor: _lerpFactor(heightFactor, other.heightFactor, value),
);
}
/// A null factor means "fill the incoming constraints", which cannot be
/// numerically interpolated with a size factor, so mixed endpoints snap
/// to the target value.
static double? _lerpFactor(double? a, double? b, double value) {
if (a == null || b == null) return b;
return (lerpDouble(a, b, value) ?? 1).clampUnderZero;
}
/// Builds the [Align], flooring any factor that overshot below zero.
///
/// [RenderPositionedBox] asserts non-negative factors, so this is a hard
/// requirement rather than a stylistic one — and it is the only place the
/// requirement applies, because it is the only place the value becomes
/// layout. [alignment] is passed through untouched, overshoot included.
@override
Widget apply(BuildContext context, Widget? child) {
return Align(
alignment: alignment,
widthFactor: _floorFactor(widthFactor),
heightFactor: _floorFactor(heightFactor),
child: child,
);
}
/// Floors a factor at zero while preserving null, which is a layout mode
/// rather than a number and must never be turned into one.
static double? _floorFactor(double? value) {
if (value == null) return null;
return value < 0 ? 0 : value;
}
@override
AlignEffect idle() => AlignEffect();
/// Field-wise sum. The alignments add through
/// [AlignmentGeometry.add], which spans absolute and directional forms;
/// the factors follow the nullability rule in the class documentation.
@override
AlignEffect operator +(AlignEffect other) => AlignEffect(
alignment: alignment.add(other.alignment),
widthFactor: VectorEffect.addLane(widthFactor, other.widthFactor),
heightFactor: VectorEffect.addLane(heightFactor, other.heightFactor),
);
/// Field-wise difference. [AlignmentGeometry] declares no binary `-`, so
/// this is expressed as `a.add(-b)`, which is the same thing and works
/// for every subtype. Routinely produces negative factors: this is the
/// displacement term of the spring solution, not a renderable state.
@override
AlignEffect operator -(AlignEffect other) => AlignEffect(
alignment: alignment.add(-other.alignment),
widthFactor: VectorEffect.subtractLane(widthFactor, other.widthFactor),
heightFactor:
VectorEffect.subtractLane(heightFactor, other.heightFactor),
);
/// Scales the alignment and any present factors by [factor]. A null
/// factor stays null: "fill the constraints" has nothing to scale.
@override
AlignEffect operator *(double factor) => AlignEffect(
alignment: alignment * factor,
widthFactor: widthFactor == null ? null : widthFactor! * factor,
heightFactor: heightFactor == null ? null : heightFactor! * factor,
);
/// The squared magnitude of the animatable values, used for settle
/// detection.
///
/// [AlignmentGeometry] keeps its components private, so the alignment is
/// read through [AlignmentGeometry.resolve] with a left-to-right
/// direction; for the purely absolute and purely directional forms the
/// choice is immaterial, and settle detection only needs a scalar that
/// vanishes with the vector. Null factors contribute nothing, matching
/// their "no numeric value" reading everywhere else here.
@override
double get magnitudeSquared {
final Alignment resolved = alignment.resolve(TextDirection.ltr);
final double width = widthFactor ?? 0;
final double height = heightFactor ?? 0;
return resolved.x * resolved.x +
resolved.y * resolved.y +
width * width +
height * height;
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is AlignEffect &&
other.runtimeType == runtimeType &&
other.alignment == alignment &&
other.widthFactor == widthFactor &&
other.heightFactor == heightFactor;
}
@override
int get hashCode => Object.hash(
alignment,
widthFactor,
heightFactor,
);
@override
String toString() =>
'AlignEffect(alignment: $alignment, widthFactor: $widthFactor, heightFactor: $heightFactor)';
}