-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathanimation_diagram.dart
360 lines (315 loc) · 10.2 KB
/
animation_diagram.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
// 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:ui' as ui;
import 'package:flutter/material.dart';
import 'diagram_step.dart';
const Duration _kAnimationDuration = Duration(seconds: 2);
const double _kFontSize = 14.0;
const double _kLogoSize = 150.0;
/// Use the class name as the basis for the caption to display, since
/// that is directly related to the name of the transition.
String _getCaption(Type type) {
return type.toString().replaceAll('Diagram', '');
}
/// Convert the caption CamelCase name into lower_with_underscores.
String getName(Type type) {
final RegExp uppercase = RegExp(r'([A-Z])');
return _getCaption(type).replaceAllMapped(uppercase, (Match match) {
if (match.start != 0) {
return '_${match.group(1)!.toLowerCase()}';
} else {
return match.group(1)!.toLowerCase();
}
});
}
/// A base class for diagrams that show explicit animation transitions, like
/// [FadeTransition]. See transitions.dart for more examples.
abstract class TransitionDiagram<T> extends StatefulWidget
with DiagramMetadata {
const TransitionDiagram({super.key, this.decorate = true});
/// Whether or not to decorate this diagram with an animation curve and top label.
final bool decorate;
/// The animation curve for both the animation and the sparkline to use.
Curve get curve;
Animation<T> buildAnimation(AnimationController controller);
Widget buildTransition(BuildContext context, Animation<T> animation);
@override
String get name => getName(runtimeType) + (decorate ? '' : '_plain');
/// The label to be shown on the top of the diagram if [decorate] is true.
String get caption => _getCaption(runtimeType);
@override
TransitionDiagramState<T> createState() => TransitionDiagramState<T>();
}
class TransitionDiagramState<T> extends State<TransitionDiagram<T>>
with TickerProviderStateMixin<TransitionDiagram<T>> {
bool selected = false;
late Animation<T> animation;
late AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: _kAnimationDuration,
vsync: this,
)..addListener(() {
setState(() {
// The animation controller is changing the animation value, so we
// need to redraw.
});
});
animation = widget.buildAnimation(_controller);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
Widget child;
if (widget.decorate) {
child = Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(
widget.caption,
style: const TextStyle(
color: Color(0xff000000),
fontStyle: FontStyle.normal,
fontSize: _kFontSize,
),
),
Expanded(
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(10.0)),
border: Border.all(color: Colors.black26),
),
constraints: const BoxConstraints.tightFor(
width: 250.0,
height: 250.0,
),
child: widget.buildTransition(context, animation),
),
),
Container(height: 25.0),
SizedBox(
width: 100.0,
height: 50.0,
child: Sparkline(curve: widget.curve, position: _controller.value),
),
],
);
} else {
child = widget.buildTransition(context, animation);
}
return GestureDetector(
onTap: () {
setState(() {
selected = !selected;
selected ? _controller.forward() : _controller.reverse();
});
},
child: Container(
// Height must be an even number for ffmpeg to be able to create a video
// from the output.
constraints: BoxConstraints.tightFor(
width: widget.decorate ? 300.0 : 250.0,
height: widget.decorate ? 378.0 : 250.0,
),
padding: EdgeInsets.only(
top: 25.0 - (widget.decorate ? _kFontSize - 1.0 : 0.0),
left: 25.0,
right: 25.0,
bottom: 25.0,
),
color: const Color(0xffffffff),
child: child,
),
);
}
}
abstract class ImplicitAnimationDiagram<T> extends StatefulWidget
with DiagramMetadata {
const ImplicitAnimationDiagram({
super.key,
this.duration = _kAnimationDuration,
});
@override
final Duration duration;
/// The animation curve for the animation to use.
Curve get curve;
Widget buildImplicitAnimation(BuildContext context, bool selected);
@override
String get name => getName(runtimeType);
String get caption => _getCaption(runtimeType);
Size get size => const Size(250.0, 250.0);
@override
ImplicitAnimationDiagramState<T> createState() =>
ImplicitAnimationDiagramState<T>();
}
class ImplicitAnimationDiagramState<T>
extends State<ImplicitAnimationDiagram<T>> {
bool selected = false;
@override
Widget build(BuildContext context) {
final Widget child = widget.buildImplicitAnimation(context, selected);
return GestureDetector(
onTap: () {
setState(() {
selected = !selected;
});
},
child: Container(
color: Colors.white,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(
widget.caption,
style: const TextStyle(
color: Color(0xff000000),
fontStyle: FontStyle.normal,
fontSize: _kFontSize,
),
),
Container(
// Height must be an even number for ffmpeg to be able to create a video
// from the output.
constraints: BoxConstraints.tightFor(
width: widget.size.width,
height: widget.size.height,
),
padding: const EdgeInsets.only(
top: 25.0 - _kFontSize - 1.0,
left: 25.0,
right: 25.0,
bottom: 25.0,
),
color: const Color(0xffffffff),
child: child,
),
],
),
),
);
}
}
/// A custom painter to draw the graph of the curve.
class SparklinePainter extends CustomPainter {
SparklinePainter(this.curve, this.position);
final Curve curve;
final double position;
static final Paint _axisPaint =
Paint()
..color = Colors.black45
..style = PaintingStyle.stroke
..strokeWidth = 2.0;
static final Paint _sparklinePaint =
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 _positionCirclePaint =
Paint()
..color = Colors.blue.shade900
..style = PaintingStyle.fill;
@override
void paint(Canvas canvas, Size size) {
assert(size != Size.zero);
const double unit = 4.0;
const double leftMargin = unit;
const double rightMargin = unit;
const double topMargin = unit;
final Rect area = Rect.fromLTRB(
leftMargin,
topMargin,
size.width - rightMargin,
size.height - topMargin,
);
final Path axes =
Path()
..moveTo(area.left, area.top) // vertical axis
..lineTo(area.left, area.bottom) // origin
..lineTo(area.right, area.bottom); // horizontal axis
canvas.drawPath(axes, _axisPaint);
final Offset activePoint = FractionalOffset(
position,
1.0 - curve.transform(position),
).withinRect(area);
// The sparkline itself.
final Path sparkline = Path()..moveTo(area.left, area.bottom);
final double stepSize =
1.0 /
(area.width *
(ui.PlatformDispatcher.instance.implicitView?.devicePixelRatio ??
1.0));
void lineToPoint(Path path, double t) {
final Offset point = FractionalOffset(
t,
1.0 - curve.transform(t),
).withinRect(area);
path.lineTo(point.dx, point.dy);
}
for (double t = 0.0; t <= position; t += stepSize) {
lineToPoint(sparkline, t);
}
// In case the last value wasn't at position due to rounding.
lineToPoint(sparkline, position);
canvas.drawPath(sparkline, _sparklinePaint);
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) {
lineToPoint(graphProgress, t);
}
// In case the last value wasn't at 1.0 due to rounding.
lineToPoint(graphProgress, 1.0);
canvas.drawPath(graphProgress, _graphProgressPaint);
canvas.drawCircle(
Offset(activePoint.dx, activePoint.dy),
4.0,
_positionCirclePaint,
);
}
@override
bool shouldRepaint(SparklinePainter oldDelegate) {
return curve != oldDelegate.curve || position != oldDelegate.position;
}
}
class Sparkline extends StatelessWidget {
const Sparkline({super.key, required this.curve, required this.position});
final Curve curve;
final double position;
@override
Widget build(BuildContext context) {
return CustomPaint(painter: SparklinePainter(curve, position));
}
}
class SampleWidget extends StatelessWidget {
const SampleWidget({super.key, this.small = false});
final bool small;
@override
Widget build(BuildContext context) {
if (small) {
return const FlutterLogo(size: _kLogoSize / 2.0);
}
return const Padding(
padding: EdgeInsets.all(8.0),
child: FlutterLogo(size: _kLogoSize),
);
}
}