This repository was archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathzoomable_widget.dart
385 lines (324 loc) · 11.2 KB
/
zoomable_widget.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import 'dart:math';
import 'package:flutter/widgets.dart';
import 'package:flutter_advanced_networkimage/src/utils.dart';
class ZoomableWidget extends StatefulWidget {
ZoomableWidget({
Key key,
this.scaleForDoubleTap: 1.0,
this.minScale: 0.7,
this.maxScale: 1.4,
this.initialScale: 1.0,
this.initialOffset: Offset.zero,
this.initialRotation: 0.0,
this.enableZoom: true,
this.panLimit: 1.0,
this.singleFingerPan: true,
this.multiFingersPan: true,
this.enableRotate: false,
this.child,
this.onTap,
this.zoomSteps: 0,
this.autoCenter: false,
this.bounceBackBoundary: true,
this.enableFling: true,
this.flingFactor: 1.0,
this.onZoomChanged,
this.resetDuration: const Duration(milliseconds: 250),
this.resetCurve: Curves.easeInOut,
}) : assert(scaleForDoubleTap != null),
assert(minScale != null),
assert(maxScale != null),
assert(initialScale != null),
assert(initialOffset != null),
assert(initialRotation != null),
assert(enableZoom != null),
assert(panLimit != null),
assert(singleFingerPan != null),
assert(multiFingersPan != null),
assert(enableRotate != null),
assert(zoomSteps != null),
assert(autoCenter != null),
assert(bounceBackBoundary != null),
assert(enableFling != null),
assert(flingFactor != null);
/// The scale size for double tap
final double scaleForDoubleTap;
/// The minimum size for scaling.
final double minScale;
/// The maximum size for scaling.
final double maxScale;
/// The initial scale.
final double initialScale;
/// The initial offset.
final Offset initialOffset;
/// The initial rotation.
final double initialRotation;
/// Allow zooming the child widget.
final bool enableZoom;
/// Allow panning with one finger.
final bool singleFingerPan;
/// Allow panning with more than one finger.
final bool multiFingersPan;
/// Allow rotating the [image].
final bool enableRotate;
/// Create a boundary with the factor.
final double panLimit;
/// The child widget that is display.
final Widget child;
/// Tap callback for this widget.
final VoidCallback onTap;
/// Allow users to zoom with double tap steps by steps.
final int zoomSteps;
/// Center offset when zooming to minimum scale.
final bool autoCenter;
/// Enable the bounce-back boundary.
final bool bounceBackBoundary;
/// Allow fling child widget after panning.
final bool enableFling;
/// Greater value create greater fling distance.
final double flingFactor;
/// When the scale value changed, the callback will be invoked.
final ValueChanged<double> onZoomChanged;
/// The duration of reset animation.
final Duration resetDuration;
/// The curve of reset animation.
final Curve resetCurve;
@override
_ZoomableWidgetState createState() => _ZoomableWidgetState();
}
class _ZoomableWidgetState extends State<ZoomableWidget> {
final GlobalKey _key = GlobalKey();
double _zoom = 1.0;
double _previousZoom = 1.0;
Offset _previousPanOffset = Offset.zero;
Offset _pan = Offset.zero;
Offset _zoomOriginOffset = Offset.zero;
double _rotation = 0.0;
double _previousRotation = 0.0;
Size _childSize = Size.zero;
Size _containerSize = Size.zero;
Duration _duration = const Duration(milliseconds: 100);
Curve _curve = Curves.easeOut;
@override
void initState() {
super.initState();
_zoom = widget.initialScale;
_pan = widget.initialOffset;
_rotation = widget.initialRotation;
}
void _onScaleStart(ScaleStartDetails details) {
if (_childSize == Size.zero) {
final RenderBox renderbox = _key.currentContext.findRenderObject();
_childSize = renderbox.size;
}
setState(() {
_zoomOriginOffset = details.focalPoint;
_previousPanOffset = _pan;
_previousZoom = _zoom;
_previousRotation = _rotation;
});
}
void _onScaleUpdate(ScaleUpdateDetails details) {
Size boundarySize = _boundarySize;
Size _marginSize = const Size(100.0, 100.0);
_duration = const Duration(milliseconds: 50);
_curve = Curves.easeOut;
setState(() {
if (widget.enableRotate)
_rotation = (_previousRotation + details.rotation).clamp(-pi, pi);
if (widget.enableZoom && details.scale != 1.0) {
_zoom = (_previousZoom * details.scale)
.clamp(widget.minScale, widget.maxScale);
if (widget.onZoomChanged != null) widget.onZoomChanged(_zoom);
}
});
if ((widget.singleFingerPan && details.scale == 1.0) ||
(widget.multiFingersPan && details.scale != 1.0)) {
Offset _panRealOffset = (details.focalPoint -
_zoomOriginOffset +
_previousPanOffset * _previousZoom) /
_zoom;
if (widget.panLimit == 0.0) {
_pan = _panRealOffset;
} else {
Offset _baseOffset = Offset(
_panRealOffset.dx
.clamp(-boundarySize.width / 2, boundarySize.width / 2),
_panRealOffset.dy
.clamp(-boundarySize.height / 2, boundarySize.height / 2),
);
Offset _marginOffset = _panRealOffset - _baseOffset;
double _widthFactor = sqrt(_marginOffset.dx.abs()) / _marginSize.width;
double _heightFactor =
sqrt(_marginOffset.dy.abs()) / _marginSize.height;
_marginOffset = Offset(
_marginOffset.dx * _widthFactor * 2,
_marginOffset.dy * _heightFactor * 2,
);
_pan = _baseOffset + _marginOffset;
}
setState(() {});
}
}
void _onScaleEnd(ScaleEndDetails details) {
Size boundarySize = _boundarySize;
_duration = widget.resetDuration;
_curve = widget.resetCurve;
final Offset velocity = details.velocity.pixelsPerSecond;
final double magnitude = velocity.distance;
if (magnitude > 800.0 * _zoom && widget.enableFling) {
final Offset direction = velocity / magnitude;
final double distance = (Offset.zero & context.size).shortestSide;
final Offset endOffset =
_pan + direction * distance * widget.flingFactor * 0.5;
_pan = Offset(
endOffset.dx.clamp(-boundarySize.width / 2, boundarySize.width / 2),
endOffset.dy.clamp(-boundarySize.height / 2, boundarySize.height / 2),
);
}
Offset _clampedOffset = Offset(
_pan.dx.clamp(-boundarySize.width / 2, boundarySize.width / 2),
_pan.dy.clamp(-boundarySize.height / 2, boundarySize.height / 2),
);
if (_zoom == widget.minScale && widget.autoCenter) {
_clampedOffset = Offset.zero;
}
setState(() => _pan = _clampedOffset);
}
Size get _boundarySize {
Size _boundarySize = Size(
(_containerSize.width == _childSize.width)
? (_containerSize.width - _childSize.width / _zoom).abs()
: (_containerSize.width - _childSize.width * _zoom).abs() / _zoom,
(_containerSize.height == _childSize.height)
? (_containerSize.height - _childSize.height / _zoom).abs()
: (_containerSize.height - _childSize.height * _zoom).abs() /
_zoom,
) *
widget.panLimit;
return _boundarySize;
}
void _handleDoubleTap() {
double _stepLength = 0.0;
_duration = widget.resetDuration;
_curve = widget.resetCurve;
if (widget.zoomSteps > 0)
_stepLength = (widget.scaleForDoubleTap - 1.0) / widget.zoomSteps;
double _tmpZoom = _zoom + _stepLength;
if (_tmpZoom > widget.scaleForDoubleTap || _stepLength == 0.0) _tmpZoom = 1.0;
setState(() {
_zoom = _tmpZoom;
if (widget.onZoomChanged != null) widget.onZoomChanged(_zoom);
_pan = Offset.zero;
_rotation = 0.0;
_previousZoom = _tmpZoom;
if (_tmpZoom == 1.0) {
_zoomOriginOffset = Offset.zero;
_previousPanOffset = Offset.zero;
}
});
}
@override
Widget build(BuildContext context) {
if (widget.child == null) return SizedBox();
return CustomMultiChildLayout(
delegate: _ZoomableWidgetLayout(),
children: <Widget>[
LayoutId(
id: _ZoomableWidgetLayout.painter,
child: _ZoomableChild(
duration: _duration,
curve: _curve,
zoom: _zoom,
panOffset: _pan,
rotation: _rotation,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
_containerSize =
Size(constraints.maxWidth, constraints.maxHeight);
return Center(
child: Container(key: _key, child: widget.child),
);
},
),
),
),
LayoutId(
id: _ZoomableWidgetLayout.gestureContainer,
child: GestureDetector(
child: Container(color: Color(0)),
onScaleStart: _onScaleStart,
onScaleUpdate: _onScaleUpdate,
onScaleEnd: widget.bounceBackBoundary ? _onScaleEnd : null,
onDoubleTap: _handleDoubleTap,
onTap: widget.onTap,
),
),
],
);
}
}
class _ZoomableWidgetLayout extends MultiChildLayoutDelegate {
_ZoomableWidgetLayout();
static final String gestureContainer = 'gesturecontainer';
static final String painter = 'painter';
@override
void performLayout(Size size) {
layoutChild(gestureContainer,
BoxConstraints.tightFor(width: size.width, height: size.height));
positionChild(gestureContainer, Offset.zero);
layoutChild(painter,
BoxConstraints.tightFor(width: size.width, height: size.height));
positionChild(painter, Offset.zero);
}
@override
bool shouldRelayout(_ZoomableWidgetLayout oldDelegate) => false;
}
class _ZoomableChild extends ImplicitlyAnimatedWidget {
const _ZoomableChild({
Duration duration,
Curve curve = Curves.linear,
@required this.zoom,
@required this.panOffset,
@required this.rotation,
@required this.child,
}) : super(duration: duration, curve: curve);
final double zoom;
final Offset panOffset;
final double rotation;
final Widget child;
@override
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() =>
_ZoomableChildState();
}
class _ZoomableChildState extends AnimatedWidgetBaseState<_ZoomableChild> {
DoubleTween _zoom;
OffsetTween _panOffset;
// OffsetTween _zoomOriginOffset;
DoubleTween _rotation;
@override
void forEachTween(visitor) {
_zoom = visitor(
_zoom, widget.zoom, (dynamic value) => DoubleTween(begin: value));
_panOffset = visitor(_panOffset, widget.panOffset,
(dynamic value) => OffsetTween(begin: value));
_rotation = visitor(_rotation, widget.rotation,
(dynamic value) => DoubleTween(begin: value));
}
@override
Widget build(BuildContext context) {
return Transform(
alignment: Alignment.center,
origin: Offset(-_panOffset.evaluate(animation).dx,
-_panOffset.evaluate(animation).dy),
transform: Matrix4.identity()
..translate(_panOffset.evaluate(animation).dx,
_panOffset.evaluate(animation).dy)
..scale(_zoom.evaluate(animation), _zoom.evaluate(animation)),
child: Transform.rotate(
angle: _rotation.evaluate(animation),
child: widget.child,
),
);
}
}