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
| // 手势驱动的弹性动画
class GestureAnimation extends StatefulWidget {
const GestureAnimation({Key? key}) : super(key: key);
@override
_GestureAnimationState createState() => _GestureAnimationState();
}
class _GestureAnimationState extends State<GestureAnimation>
with TickerProviderStateMixin {
late AnimationController _animationController;
late AnimationController _bounceController;
final GlobalKey _cardKey = GlobalKey();
double _rotation = 0;
double _scale = 1;
Offset _position = const Offset(0, 0);
bool _isDragging = false;
@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: const Duration(milliseconds: 300),
vsync: this,
);
_bounceController = AnimationController(
duration: const Duration(milliseconds: 600),
vsync: this,
);
}
void _onPanStart(DragStartDetails details) {
setState(() {
_isDragging = true;
});
_animationController.stop();
}
void _onPanUpdate(DragUpdateDetails details) {
final RenderBox renderBox = context.findRenderObject() as RenderBox;
final localPosition = renderBox.globalToLocal(details.globalPosition);
setState(() {
_position = localPosition - const Offset(200, 150);
// 根据拖拽距离计算旋转
_rotation = _position.dx * 0.01;
// 拖拽时略微放大
_scale = 1.05;
});
}
void _onPanEnd(DragEndDetails details) {
setState(() {
_isDragging = false;
});
// 计算回弹动画
final velocity = details.velocity.pixelsPerSecond;
final distance = (_position.distance + 100).clamp(0.0, 300.0);
// 使用弹性动画返回原位
_animationController.animateTo(0).then((_) {
// 触发弹跳效果
_bounceController.forward().then((_) {
_bounceController.reverse();
});
});
// 添加惯性效果
_animateWithInertia(velocity, distance);
}
void _animateWithInertia(Offset velocity, double distance) {
final inertiaAnimation = Tween<Offset>(
begin: _position,
end: Offset.zero,
).animate(CurvedAnimation(
parent: _animationController,
curve: Curves.elasticOut,
));
inertiaAnimation.addListener(() {
setState(() {
_position = inertiaAnimation.value;
_rotation = _position.dx * 0.01;
_scale = 1 + distance * 0.0002 * (1 - inertiaAnimation.value.distance);
});
});
_animationController.forward(from: _animationController.value);
}
void _onTap() {
// 点击时的弹跳动画
_bounceController.forward().then((_) {
_bounceController.reverse();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Gesture Animation')),
body: Center(
child: GestureDetector(
onPanStart: _onPanStart,
onPanUpdate: _onPanUpdate,
onPanEnd: _onPanEnd,
onTap: _onTap,
child: Container(
width: 400,
height: 400,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade300),
borderRadius: BorderRadius.circular(20),
),
child: Stack(
children: [
// 参考网格
CustomPaint(
size: const Size(400, 400),
painter: GridPainter(),
),
// 可拖拽卡片
AnimatedBuilder(
animation: Listenable.merge([
_animationController,
_bounceController,
]),
builder: (context, child) {
return Transform.translate(
offset: _position,
child: Transform.rotate(
angle: _rotation,
child: Transform.scale(
scale: _scale * (1 + _bounceController.value * 0.1),
child: Container(
key: _cardKey,
width: 300,
height: 200,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.blue.shade400,
Colors.purple.shade600,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(_isDragging ? 0.3 : 0.2),
blurRadius: _isDragging ? 20 : 10,
offset: Offset(
_isDragging ? _position.dx * 0.1 : 0,
_isDragging ? _position.dy * 0.1 : 5,
),
),
],
),
child: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.touch_app,
color: Colors.white,
size: 48,
),
SizedBox(height: 8),
Text(
'Drag Me!',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
),
),
);
},
),
],
),
),
),
),
);
}
}
// 网格绘制器
class GridPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.grey.shade300
..strokeWidth = 0.5;
const gridSize = 50.0;
// 绘制垂直线
for (double x = 0; x < size.width; x += gridSize) {
canvas.drawLine(
Offset(x, 0),
Offset(x, size.height),
paint,
);
}
// 绘制水平线
for (double y = 0; y < size.height; y += gridSize) {
canvas.drawLine(
Offset(0, y),
Offset(size.width, y),
paint,
);
}
// 绘制中心十字
final centerPaint = Paint()
..color = Colors.grey.shade500
..strokeWidth = 1;
canvas.drawLine(
const Offset(200, 0),
const Offset(200, 400),
centerPaint,
);
canvas.drawLine(
const Offset(0, 200),
const Offset(400, 200),
centerPaint,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
|