-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreeBox.dart
120 lines (109 loc) · 2.84 KB
/
FreeBox.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
```
import 'package:flutter/material.dart';
class FreeBox extends StatefulWidget {
FreeBox({
@required this.children,
@required this.backgroundColor,
@required this.boxWidth,
@required this.boxHeight,
@required this.eventWidth,
@required this.eventHeight,
});
final List<Positioned> children;
final Color backgroundColor;
final double boxWidth;
final double boxHeight;
final double eventWidth;
final double eventHeight;
@override
State<StatefulWidget> createState() {
return _FreeBox();
}
}
class _FreeBox extends State<FreeBox> {
double _scale = 1;
double _lastScale = 1;
Offset _offset = Offset(0, 0);
Offset _lastOffset = Offset(0, 0);
@override
Widget build(BuildContext context) {
return GestureDetector(
onScaleStart: (details) {
_lastScale = 1;
_lastOffset = details.localFocalPoint;
},
onScaleUpdate: (details) {
double deltaScale = details.scale - _lastScale;
_scale *= 1 + deltaScale;
_lastScale = details.scale;
Offset pivotDeltaOffset = (_offset - details.localFocalPoint) * deltaScale;
_offset += pivotDeltaOffset;
Offset deltaOffset = details.localFocalPoint - _lastOffset;
_offset += deltaOffset;
_lastOffset = details.localFocalPoint;
setState(() {});
},
child: Container(
alignment: Alignment.center,
color: widget.backgroundColor,
//视觉区域、触摸区域
width: widget.boxWidth,
height: widget.boxHeight,
child: Stack(
children: <Widget>[
Positioned(
//内部事件区域
width: widget.eventWidth,
height: widget.eventHeight,
child: Transform.translate(
offset: _offset,
child: Transform.scale(
alignment: Alignment.topLeft,
scale: _scale,
child: Stack(
children: widget.children,
),
),
),
),
],
),
),
);
}
}
class MyButton extends StatefulWidget {
MyButton({@required this.child});
final Widget child;
@override
State<StatefulWidget> createState() {
return _MyButton();
}
}
class _MyButton extends State<MyButton> {
Color _color = Colors.yellow;
@override
Widget build(BuildContext context) {
return Listener(
child: Container(
width: 100,
height: 100,
color: _color,
child: widget.child,
),
onPointerDown: (event) {
_color = Colors.grey;
setState(() {});
},
onPointerCancel: (event) {
_color = Colors.yellow;
setState(() {});
},
onPointerUp: (event) {
_color = Colors.yellow;
setState(() {});
},
);
}
}
```