-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathprimitives.dart
157 lines (141 loc) · 4.23 KB
/
primitives.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
import 'dart:math' as math;
import 'package:flutter/material.dart';
class MotionDemo extends StatefulWidget {
const MotionDemo({super.key});
@override
State<MotionDemo> createState() => _MotionDemoState();
}
class _MotionDemoState extends State<MotionDemo>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _rotationAnimation;
double _currentRotation = 0.0;
double _lastSliderValue = 0.0;
double _lastUpdateTime = 0.0;
bool _isSliding = false;
double _velocity = 0.0;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1000),
);
_rotationAnimation = Tween<double>(
begin: 0.0,
end: 0.0,
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.decelerate,
))
..addListener(() {
setState(() {
_currentRotation = _rotationAnimation.value;
});
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _onSliderChangeStart(double value) {
_isSliding = true;
_controller.stop();
_lastUpdateTime = DateTime.now().millisecondsSinceEpoch / 1000.0;
_lastSliderValue = value;
}
void _onSliderChanged(double value) {
final now = DateTime.now().millisecondsSinceEpoch / 1000.0;
final dt = now - _lastUpdateTime;
if (dt > 0 && _isSliding) {
_velocity = (value - _lastSliderValue) / dt;
_currentRotation = value;
}
_lastUpdateTime = now;
_lastSliderValue = value;
setState(() {});
}
void _onSliderChangeEnd(double value) {
_isSliding = false;
// Only animate if there's significant velocity
if (_velocity.abs() > 50) {
// Threshold for "fast" movement
final velocityDirection = _velocity.sign;
final momentumDistance =
_velocity.abs() * 0.5; // Scale factor for momentum
_rotationAnimation = Tween<double>(
begin: _currentRotation,
end: _currentRotation + (momentumDistance * velocityDirection),
).animate(CurvedAnimation(
parent: _controller,
// Use a physics-based curve for momentum
curve: Curves.easeOutCubic,
));
_controller
..duration = Duration(milliseconds: (momentumDistance * 2).round())
..forward(from: 0.0);
}
// Reset velocity
_velocity = 0.0;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Center(
child: Transform.rotate(
angle: _currentRotation * math.pi / 180,
child: Container(
width: 150,
height: 150,
decoration: BoxDecoration(
border: Border.all(
color: Colors.white,
width: 2,
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
children: [
Row(
children: [
const SizedBox(width: 80, child: Text('rotate')),
Expanded(
child: Slider(
value: _currentRotation,
min: -180,
max: 180,
onChangeStart: _onSliderChangeStart,
onChanged: _onSliderChanged,
onChangeEnd: _onSliderChangeEnd,
),
),
SizedBox(
width: 50,
child: Text(
_currentRotation.toStringAsFixed(1),
textAlign: TextAlign.right,
),
),
],
),
],
),
),
],
),
),
);
}
}