/
interactive_box_flutter.dart
93 lines (80 loc) · 2.37 KB
/
interactive_box_flutter.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
import 'package:flutter/material.dart';
import 'app.dart';
void main() {
runApp(const ExampleApp(page: Page()));
}
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> with SingleTickerProviderStateMixin {
static final _distanceColorTween =
ColorTween(begin: Colors.blue, end: Colors.green);
late final _controller = AnimationController(
duration: const Duration(milliseconds: 300),
vsync: this,
);
final _alignmentTween = AlignmentTween(end: Alignment.center);
final _colorTween = ColorTween(end: _distanceColorTween.begin);
late var _alignment = _alignmentTween.end!;
late var _color = _distanceColorTween.begin!;
@override
void initState() {
super.initState();
final curveTween = CurveTween(curve: Curves.ease);
final alignmentAnimation =
_alignmentTween.chain(curveTween).animate(_controller);
final colorAnimation = _colorTween.chain(curveTween).animate(_controller);
_controller.addListener(() {
setState(() {
_alignment = alignmentAnimation.value;
_color = colorAnimation.value!;
});
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size / 2;
return Scaffold(
body: GestureDetector(
onPanUpdate: (details) {
_controller.stop();
setState(() {
_alignment += Alignment(
details.delta.dx / size.width,
details.delta.dy / size.height,
);
final distance = Offset(_alignment.x, _alignment.y).distance;
_color = _distanceColorTween.transform(distance)!;
});
},
onPanEnd: (_) {
_alignmentTween.begin = _alignment;
_colorTween.begin = _color;
_controller.forward(from: 0);
},
child: Align(
alignment: _alignment,
child: SizedBox.square(
dimension: 400,
child: ColoredBox(
color: _color,
child: const Center(
child: Text(
'Drag me!',
style: TextStyle(color: Colors.white),
),
),
),
),
),
),
);
}
}