-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake_animate_cube.dart
More file actions
187 lines (166 loc) · 5.44 KB
/
fake_animate_cube.dart
File metadata and controls
187 lines (166 loc) · 5.44 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
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class FakeAnimateCube extends StatefulWidget {
static Map<String, WidgetBuilder> routeNameMap = {
"/FakeAnimateCube": (context) => FakeAnimateCube()
};
@override
_FakeAnimateCubeState createState() => _FakeAnimateCubeState();
}
class _FakeAnimateCubeState extends State<FakeAnimateCube>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation<Color> _color;
Animation<double> _progress;
@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: const Duration(milliseconds: 500), vsync: this);
_animationController.addListener(() {
setState(() {});
});
_color = ColorTween(begin: Colors.black, end: Colors.white)
.animate(_animationController);
_progress = Tween(begin: -1.0, end: 1.0).animate(_animationController);
_animationController.repeat(reverse: true);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
color: _color.value,
child: buildPerspectiveCubes(),
);
}
buildPerspectiveCubes() {
var size = MediaQuery.of(context).size;
var landscape = size.width / size.height > 1;
return landscape
? Row(
children: [
Expanded(
child: _Cube(size.width / 2, size.height, _progress.value)),
Expanded(
child: _Cube(size.width / 2, size.height, _progress.value))
],
)
: Column(
children: [
Expanded(
child: _Cube(size.width, size.height / 2, _progress.value)),
Expanded(
child: _Cube(size.width, size.height / 2, _progress.value)),
],
);
}
}
class _Cube extends StatelessWidget {
final double width;
final double height;
final double progress;
_Cube(this.width, this.height, this.progress);
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: _CubePainter(progress),
willChange: true,
size: Size(width, height),
);
}
}
class _CubePainter extends CustomPainter {
static final cubePoints = [
[0.0, 0.0],
[1.0, 0.0],
[1.0, -1.0],
[0.0, -1.0],
[0.7071067811865474, -0.7071067811865477], //cos45º, sin45º
[0.7071067811865474, -1 - 0.7071067811865477], //cos45º, sin45º
[1 + 0.7071067811865474, -0.7071067811865477], //cos45º, sin45º
[1 + 0.7071067811865474, -1 - 0.7071067811865477], //cos45º, sin45º
];
static final cubeFaces = [
[cubePoints[4], cubePoints[5], cubePoints[7], cubePoints[6]], //Back
[cubePoints[0], cubePoints[4], cubePoints[6], cubePoints[1]], //Bottom
[cubePoints[0], cubePoints[3], cubePoints[5], cubePoints[4]], //Left
[cubePoints[1], cubePoints[2], cubePoints[7], cubePoints[6]], //Right
[cubePoints[2], cubePoints[3], cubePoints[5], cubePoints[7]], //UP
[cubePoints[0], cubePoints[3], cubePoints[2], cubePoints[1]], //FRONT
];
static final edgeWidth = 4.0;
static final shadowWidth = 2.0;
final double progress;
_CubePainter(this.progress);
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..strokeWidth = edgeWidth
..style = PaintingStyle.stroke
..color = Colors.blue
..isAntiAlias = true
..strokeJoin = StrokeJoin.round;
Path path = Path();
double baseWidth = min(size.width, size.height) / 3;
for (int i = 0; i < cubeFaces.length; i++) {
var p = cubeFaces[i];
path.reset();
for (var j = 0, l = p.length; j < l; j++) {
double x = p[j][0] * baseWidth + size.width / 4;
double y = p[j][1] * baseWidth + size.height * 2 / 3;
if (j == 0) {
path.moveTo(x, y);
} else {
path.lineTo(x, y);
// appendShadowLine(
// canvas,
// Offset(p[j - 1][0] * baseWidth + size.width / 4,
// p[j - 1][1] * baseWidth + size.height / 2),
// Offset(x, y));
}
}
path.close();
canvas.drawPath(path, paint);
if (!kIsWeb) {
// this would slow in web platform
canvas.drawShadow(path, Colors.black, 1.0, true);
}
}
}
// void appendShadowLine(Canvas canvas, Offset start, Offset end) {
// final offset = Offset(edgeWidth / 2 + shadowWidth, 0);
//
// Paint paint = Paint()
// ..strokeWidth = shadowWidth
// ..strokeJoin = StrokeJoin.round
// ..style = PaintingStyle.stroke
// ..shader = ui.Gradient.linear(
// start,
// end,
// [Colors.white, Colors.white, Colors.black, Colors.black],
// [0, 0.1 + 0.9 * progress, 0.9 + 0.1 * progress, 1.0]);
//
// Paint reversePaint = Paint()
// ..strokeWidth = shadowWidth
// ..strokeJoin = StrokeJoin.round
// ..style = PaintingStyle.stroke
// ..shader = ui.Gradient.linear(
// start,
// end,
// [Colors.black, Colors.black, Colors.white, Colors.white],
// [0, 0.1 + 0.9 * progress, 0.9 + 0.1 * progress, 1.0]);
//
// canvas.drawLine(start - offset, end - offset, paint);
// canvas.drawLine(start + offset, end + offset, reversePaint);
// }
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}