-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathcurve.dart
702 lines (660 loc) · 19.6 KB
/
curve.dart
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'diagram_step.dart';
const double _kFontSize = 14.0;
const Duration _kCurveAnimationDuration = Duration(seconds: 2);
/// A custom painter to draw the graph of the curve.
class CurveDescription extends CustomPainter {
CurveDescription(this.caption, this.curve, this.position)
: _caption = _createLabelPainter(caption, color: Colors.black);
final String caption;
final Curve curve;
final double position;
static final TextPainter _t = _createLabelPainter(
't',
style: FontStyle.italic,
);
static final TextPainter _x = _createLabelPainter(
'x',
style: FontStyle.italic,
);
static final TextPainter _zero = _createLabelPainter('0.0');
static final TextPainter _one = _createLabelPainter('1.0');
final TextPainter _caption;
static TextPainter _createLabelPainter(
String label, {
FontStyle style = FontStyle.normal,
Color color = Colors.black45,
}) {
final TextPainter result = TextPainter(
textDirection: TextDirection.ltr,
text: TextSpan(
text: label,
style: TextStyle(color: color, fontStyle: style, fontSize: _kFontSize),
),
);
result.layout();
return result;
}
static final Paint _axisPaint =
Paint()
..color = Colors.black45
..style = PaintingStyle.stroke
..strokeWidth = 2.0;
static final Paint _positionPaint =
Paint()
..color = Colors.black45
..style = PaintingStyle.stroke
..strokeWidth = 0.0;
static final Paint _dashPaint =
Paint()
..color = Colors.black45
..style = PaintingStyle.stroke
..strokeWidth = 0.0;
static final Paint _graphPaint =
Paint()
..color = Colors.blue.shade900
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeWidth = 4.0;
static final Paint _graphProgressPaint =
Paint()
..color = Colors.black26
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeWidth = 4.0;
static final Paint _valueMarkerPaint =
Paint()
..color = const Color(0xffA02020)
..style = PaintingStyle.fill;
static final Paint _positionCirclePaint =
Paint()
..color = Colors.blue.shade900
..style = PaintingStyle.fill;
@override
void paint(Canvas canvas, Size size) {
assert(size != Size.zero);
final double unit = _zero.width / 4.0;
final double leftMargin = unit * 6.0;
final double rightMargin = 3.0 * unit + _t.width;
final double bottomMargin = unit / 2.0;
final double verticalHeadroom = size.height * 0.2;
final double markerWidth = unit * 3.0;
final Rect area = Rect.fromLTRB(
leftMargin,
verticalHeadroom,
size.width - rightMargin,
size.height - verticalHeadroom,
);
final Path axes =
Path()
..moveTo(area.left - unit, area.top) // vertical axis 1.0 tick
..lineTo(area.left, area.top) // vertical axis
..lineTo(area.left, area.bottom) // origin
..lineTo(area.right, area.bottom) // horizontal axis
..lineTo(area.right, area.bottom + unit); // horizontal axis 1.0 tick
canvas.drawPath(axes, _axisPaint);
final Path dashLine = Path();
final double delta = 8.0 / area.width;
assert(delta > 0.0);
for (double t = 0.0; t < 1.0; t += delta) {
final Offset point1 = FractionalOffset(t, 0.0).withinRect(area);
final Offset point2 = FractionalOffset(
t + delta / 2.0,
0.0,
).withinRect(area);
dashLine
..moveTo(point1.dx, point1.dy)
..lineTo(point2.dx, point2.dy);
}
canvas.drawPath(dashLine, _dashPaint);
_one.paint(
canvas,
Offset(
area.left - leftMargin + (_zero.width - _one.width),
area.top - _one.height / 2.0,
),
);
_one.paint(
canvas,
Offset(area.right - _one.width / 2.0, area.bottom + bottomMargin + unit),
);
_x.paint(canvas, Offset(area.left + _x.width, area.top));
_t.paint(
canvas,
Offset(area.right - _t.width, area.bottom - _t.height - unit / 2.0),
);
_caption.paint(
canvas,
Offset(
leftMargin + (area.width - _caption.width) / 2.0,
size.height - (verticalHeadroom + _caption.height) / 2.0,
),
);
final Offset activePoint = FractionalOffset(
position,
1.0 - curve.transform(position),
).withinRect(area);
// Skip drawing the tracing line if we're at 0.0 because we want the
// initial paused state to not include the position indicators. They just
// add clutter before the animation is started.
if (position != 0.0) {
final Path positionLine =
Path()
..moveTo(activePoint.dx, area.bottom)
..lineTo(activePoint.dx, area.top); // vertical pointer from base
canvas.drawPath(positionLine, _positionPaint);
final Path valueMarker =
Path()
..moveTo(area.right + unit, activePoint.dy)
..lineTo(area.right + unit * 2.0, activePoint.dy - unit)
..lineTo(
area.right + unit * 2.0 + markerWidth,
activePoint.dy - unit,
)
..lineTo(
area.right + unit * 2.0 + markerWidth,
activePoint.dy + unit,
)
..lineTo(area.right + unit * 2.0, activePoint.dy + unit)
..lineTo(area.right + unit, activePoint.dy);
canvas.drawPath(valueMarker, _valueMarkerPaint);
}
final Path graph = Path()..moveTo(area.left, area.bottom);
final double stepSize =
1.0 /
(area.width *
(ui.PlatformDispatcher.instance.implicitView?.devicePixelRatio ??
1.0));
for (
double t = 0.0;
t <= (position == 0.0 ? 1.0 : position);
t += stepSize
) {
final Offset point = FractionalOffset(
t,
1.0 - curve.transform(t),
).withinRect(area);
graph.lineTo(point.dx, point.dy);
}
canvas.drawPath(graph, _graphPaint);
if (position != 0.0) {
final Offset startPoint = FractionalOffset(
position,
1.0 - curve.transform(position),
).withinRect(area);
final Path graphProgress = Path()..moveTo(startPoint.dx, startPoint.dy);
for (double t = position; t <= 1.0; t += stepSize) {
final Offset point = FractionalOffset(
t,
1.0 - curve.transform(t),
).withinRect(area);
graphProgress.lineTo(point.dx, point.dy);
}
canvas.drawPath(graphProgress, _graphProgressPaint);
canvas.drawCircle(
Offset(activePoint.dx, activePoint.dy),
4.0,
_positionCirclePaint,
);
}
}
@override
bool shouldRepaint(CurveDescription oldDelegate) {
return caption != oldDelegate.caption || curve != oldDelegate.curve;
}
}
/// A sample tile that shows the effect of a curve on translation.
class TranslateSampleTile extends StatelessWidget {
const TranslateSampleTile({
super.key,
required this.animation,
required this.name,
});
static const double blockHeight = 20.0;
static const double blockWidth = 30.0;
static const double containerSize = 48.0;
final Animation<double> animation;
final String name;
Widget mutate({required Widget child}) {
return Transform.translate(
offset: Offset(0.0, 13.0 - animation.value * 26.0),
child: child,
);
}
@override
Widget build(BuildContext context) {
const BorderRadius outerRadius = BorderRadius.all(Radius.circular(8.0));
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(6.0),
child: ClipRRect(
borderRadius: outerRadius,
// ignore: avoid_unnecessary_containers
child: Container(
width: containerSize,
height: containerSize,
alignment: Alignment.center,
padding: const EdgeInsets.all(4.0),
decoration: BoxDecoration(
borderRadius: outerRadius,
border: Border.all(color: Colors.black45),
),
child: mutate(
child: Container(
decoration: const BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(4.0)),
),
width: blockWidth,
height: blockHeight,
),
),
),
),
),
Text(
name,
textAlign: TextAlign.center,
style: Theme.of(
context,
).textTheme.bodyLarge!.copyWith(color: Colors.black, fontSize: 12.0),
),
],
);
}
}
/// A sample tile that shows the effect of a curve on rotation.
class RotateSampleTile extends TranslateSampleTile {
const RotateSampleTile({
super.key,
required super.animation,
required super.name,
});
@override
Widget mutate({required Widget child}) {
return Transform.rotate(
angle: animation.value * math.pi / 2.0,
child: child,
);
}
}
/// A sample tile that shows the effect of a curve on scale.
class ScaleSampleTile extends TranslateSampleTile {
const ScaleSampleTile({
super.key,
required super.animation,
required super.name,
});
@override
Widget mutate({required Widget child}) {
return Transform.scale(scale: math.max(animation.value, 0.0), child: child);
}
}
/// A sample tile that shows the effect of a curve on opacity.
class OpacitySampleTile extends TranslateSampleTile {
const OpacitySampleTile({
super.key,
required super.animation,
required super.name,
});
@override
Widget mutate({required Widget child}) {
return Opacity(opacity: animation.value.clamp(0.0, 1.0), child: child);
}
}
class CurveDiagram extends StatefulWidget with DiagramMetadata {
const CurveDiagram({
required String name,
required this.caption,
this.duration = _kCurveAnimationDuration,
required this.curve,
super.key,
}) : name = 'curve_$name';
@override
final String name;
final String caption;
final Curve curve;
@override
final Duration duration;
@override
CurveDiagramState createState() {
return CurveDiagramState();
}
}
class CurveDiagramState extends State<CurveDiagram>
with TickerProviderStateMixin<CurveDiagram> {
late AnimationController controller;
late CurvedAnimation animation;
@override
void didUpdateWidget(CurveDiagram oldWidget) {
super.didUpdateWidget(oldWidget);
controller.value = 0.0;
animation = CurvedAnimation(curve: widget.curve, parent: controller);
controller.forward();
}
@override
void initState() {
super.initState();
controller = AnimationController(duration: widget.duration, vsync: this)
..addListener(() {
setState(() {});
});
animation = CurvedAnimation(curve: widget.curve, parent: controller);
controller.forward();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final CurveDescription description = CurveDescription(
widget.caption,
widget.curve,
controller.value,
);
return Container(
padding: const EdgeInsets.all(7.0),
color: Colors.white,
child: ConstrainedBox(
constraints: BoxConstraints.tight(const Size(450.0, 178.0)),
child: Row(
children: <Widget>[
ConstrainedBox(
constraints: BoxConstraints.tight(const Size(300.0, 178.0)),
key: UniqueKey(),
child: CustomPaint(painter: description),
),
Container(
constraints: BoxConstraints.tight(const Size(150.0, 178.0)),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TranslateSampleTile(
animation: animation,
name: 'translation',
),
RotateSampleTile(animation: animation, name: 'rotation'),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ScaleSampleTile(animation: animation, name: 'scale'),
OpacitySampleTile(animation: animation, name: 'opacity'),
],
),
],
),
),
],
),
),
);
}
}
class CurveDiagramStep extends DiagramStep {
@override
final String category = 'animation';
final List<CurveDiagram> _diagrams = <CurveDiagram>[
const CurveDiagram(
name: 'bounce_in',
caption: 'Curves.bounceIn',
curve: Curves.bounceIn,
),
const CurveDiagram(
name: 'bounce_in_out',
caption: 'Curves.bounceInOut',
curve: Curves.bounceInOut,
),
const CurveDiagram(
name: 'bounce_out',
caption: 'Curves.bounceOut',
curve: Curves.bounceOut,
),
const CurveDiagram(
name: 'decelerate',
caption: 'Curves.decelerate',
curve: Curves.decelerate,
),
const CurveDiagram(
name: 'ease',
caption: 'Curves.ease',
curve: Curves.ease,
),
const CurveDiagram(
name: 'ease_in',
caption: 'Curves.easeIn',
curve: Curves.easeIn,
),
const CurveDiagram(
name: 'ease_in_sine',
caption: 'Curves.easeInSine',
curve: Curves.easeInSine,
),
const CurveDiagram(
name: 'ease_in_quad',
caption: 'Curves.easeInQuad',
curve: Curves.easeInSine,
),
const CurveDiagram(
name: 'ease_in_cubic',
caption: 'Curves.easeInCubic',
curve: Curves.easeInCubic,
),
const CurveDiagram(
name: 'ease_in_quart',
caption: 'Curves.easeInQuart',
curve: Curves.easeInQuart,
),
const CurveDiagram(
name: 'ease_in_quint',
caption: 'Curves.easeInQuint',
curve: Curves.easeInQuint,
),
const CurveDiagram(
name: 'ease_in_expo',
caption: 'Curves.easeInExpo',
curve: Curves.easeInExpo,
),
const CurveDiagram(
name: 'ease_in_circ',
caption: 'Curves.easeInCirc',
curve: Curves.easeInCirc,
),
const CurveDiagram(
name: 'ease_in_back',
caption: 'Curves.easeInBack',
curve: Curves.easeInBack,
),
const CurveDiagram(
name: 'ease_in_out',
caption: 'Curves.easeInOut',
curve: Curves.easeInOut,
),
const CurveDiagram(
name: 'ease_in_out_sine',
caption: 'Curves.easeInOutSine',
curve: Curves.easeInOutSine,
),
const CurveDiagram(
name: 'ease_in_out_quad',
caption: 'Curves.easeInOutQuad',
curve: Curves.easeInOutSine,
),
const CurveDiagram(
name: 'ease_in_out_cubic',
caption: 'Curves.easeInOutCubic',
curve: Curves.easeInOutCubic,
),
const CurveDiagram(
name: 'ease_in_out_cubic_emphasized',
caption: 'Curves.easeInOutCubicEmphasized',
curve: Curves.easeInOutCubicEmphasized,
),
const CurveDiagram(
name: 'ease_in_out_quart',
caption: 'Curves.easeInOutQuart',
curve: Curves.easeInOutQuart,
),
const CurveDiagram(
name: 'ease_in_out_quint',
caption: 'Curves.easeInOutQuint',
curve: Curves.easeInOutQuint,
),
const CurveDiagram(
name: 'ease_in_out_expo',
caption: 'Curves.easeInOutExpo',
curve: Curves.easeInOutExpo,
),
const CurveDiagram(
name: 'ease_in_out_circ',
caption: 'Curves.easeInOutCirc',
curve: Curves.easeInOutCirc,
),
const CurveDiagram(
name: 'ease_in_out_back',
caption: 'Curves.easeInOutBack',
curve: Curves.easeInOutBack,
),
const CurveDiagram(
name: 'ease_out',
caption: 'Curves.easeOut',
curve: Curves.easeOut,
),
const CurveDiagram(
name: 'ease_out_sine',
caption: 'Curves.easeOutSine',
curve: Curves.easeOutSine,
),
const CurveDiagram(
name: 'ease_out_quad',
caption: 'Curves.easeOutQuad',
curve: Curves.easeOutSine,
),
const CurveDiagram(
name: 'ease_out_cubic',
caption: 'Curves.easeOutCubic',
curve: Curves.easeOutCubic,
),
const CurveDiagram(
name: 'ease_out_quart',
caption: 'Curves.easeOutQuart',
curve: Curves.easeOutQuart,
),
const CurveDiagram(
name: 'ease_out_quint',
caption: 'Curves.easeOutQuint',
curve: Curves.easeOutQuint,
),
const CurveDiagram(
name: 'ease_out_expo',
caption: 'Curves.easeOutExpo',
curve: Curves.easeOutExpo,
),
const CurveDiagram(
name: 'ease_out_circ',
caption: 'Curves.easeOutCirc',
curve: Curves.easeOutCirc,
),
const CurveDiagram(
name: 'ease_out_back',
caption: 'Curves.easeOutBack',
curve: Curves.easeOutBack,
),
const CurveDiagram(
name: 'elastic_in',
caption: 'Curves.elasticIn',
curve: Curves.elasticIn,
),
const CurveDiagram(
name: 'elastic_in_out',
caption: 'Curves.elasticInOut',
curve: Curves.elasticInOut,
),
const CurveDiagram(
name: 'elastic_out',
caption: 'Curves.elasticOut',
curve: Curves.elasticOut,
),
const CurveDiagram(
name: 'fast_out_slow_in',
caption: 'Curves.fastOutSlowIn',
curve: Curves.fastOutSlowIn,
),
const CurveDiagram(
name: 'slow_middle',
caption: 'Curves.slowMiddle',
curve: Curves.slowMiddle,
),
CurveDiagram(
name: 'flipped',
caption: 'Curves.bounceIn.flipped',
curve: Curves.bounceIn.flipped,
),
const CurveDiagram(
name: 'flipped_curve',
caption: 'FlippedCurve(Curves.bounceIn)',
curve: FlippedCurve(Curves.bounceIn),
),
const CurveDiagram(
name: 'interval',
caption: 'Interval(0.25, 0.75)',
curve: Interval(0.25, 0.75),
),
const CurveDiagram(name: 'split', caption: 'Split(0.5)', curve: Split(0.5)),
const CurveDiagram(
name: 'linear',
caption: 'Curves.linear',
curve: Curves.linear,
),
const CurveDiagram(
name: 'sawtooth',
caption: 'SawTooth(3)',
curve: SawTooth(3),
),
const CurveDiagram(
name: 'threshold',
caption: 'Threshold(0.75)',
curve: Threshold(0.75),
),
const CurveDiagram(
name: 'linear_to_ease_out',
caption: 'Curves.linearToEaseOut',
curve: Curves.linearToEaseOut,
),
const CurveDiagram(
name: 'ease_in_to_linear',
caption: 'Curves.easeInToLinear',
curve: Curves.easeInToLinear,
),
const CurveDiagram(
name: 'fast_linear_to_slow_ease_in',
caption: 'Curves.fastLinearToSlowEaseIn',
curve: Curves.fastLinearToSlowEaseIn,
),
const CurveDiagram(
name: 'fast_ease_in_to_slow_ease_out',
caption: 'Curves.fastEaseInToSlowEaseOut',
curve: Curves.fastEaseInToSlowEaseOut,
),
];
@override
Future<List<CurveDiagram>> get diagrams async => _diagrams;
}