forked from bluefireteam/photo_view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoto_view.dart
316 lines (283 loc) · 9.36 KB
/
photo_view.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
library photo_view;
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:photo_view/src/photo_view_image_wrapper.dart';
import 'package:photo_view/src/photo_view_scale_boundaries.dart';
import 'package:photo_view/src/photo_view_scale_state.dart';
import 'package:after_layout/after_layout.dart';
export 'package:photo_view/src/photo_view_computed_scale.dart';
export 'package:photo_view/photo_view_gallery.dart';
typedef PhotoViewScaleStateChangedCallback = void Function(
PhotoViewScaleState scaleState);
/// A [StatefulWidget] that contains all the photo view rendering elements.
///
/// Internally, the image is rendered within an [Image] widget.
///
/// To use along a hero animation, provide [heroTag] param.
///
/// Sample code:
///
/// ```
/// PhotoView(
/// imageProvider: imageProvider,
/// loadingChild: new LoadingText(),
/// backgroundColor: Colors.white,
/// minScale: PhotoViewComputedScale.contained,
/// maxScale: 2.0,
/// gaplessPlayback: false,
/// size:MediaQuery.of(context).size,
/// heroTag: "someTag"
/// );
/// ```
///
class PhotoView extends StatefulWidget {
/// Creates a widget that displays an zoomable image.
///
/// To show an image from the network or from an asset bundle, use their respective
/// image providers, ie: [AssetImage] or [NetworkImage]
///
/// The [maxScale] and [minScale] arguments may be [double] or a [PhotoViewComputedScale] constant
///
/// Sample using [maxScale] and [minScale]
///
/// ```
/// PhotoView(
/// imageProvider: imageProvider,
/// minScale: PhotoViewComputedScale.contained * 1.8,
/// maxScale: PhotoViewComputedScale.covered * 1.1
/// );
/// ```
/// [size] is used to define the viewPort size in which the image will be
/// scaled to. This argument is rarely used. By befault is the size of the
/// screen. [PhotoViewInline] defines is as the size of the widget.
///
/// The argument [gaplessPlayback] is used to continue showing the old image
/// (`true`), or briefly show nothing (`false`), when the [imageProvider]
/// changes.By default it's set to `false`.
///
/// To use within an hero animation, specify [heroTag]. When [heroTag] is
/// specified, the image provider retrieval process should be sync.
///
/// Sample using hero animation
/// ```
/// // screen1
/// ...
/// Hero(
/// tag: "someTag",
/// child: Image.asset(
/// "assets/large-image.jpg",
/// width: 150.0
/// ),
/// )
/// // screen2
/// ...
/// child: PhotoView(
/// imageProvider: AssetImage("assets/large-image.jpg"),
/// heroTag: "someTag",
/// )
/// ```
///
const PhotoView(
{Key key,
@required this.imageProvider,
this.loadingChild,
this.backgroundColor = const Color.fromRGBO(0, 0, 0, 1.0),
this.minScale,
this.maxScale,
this.gaplessPlayback = false,
this.size,
this.heroTag,
this.scaleStateChangedCallback,
this.useImage = true,
this.zoomableWidget})
: super(key: key);
/// Given a [imageProvider] it resolves into an zoomable image widget using. It
/// is required
final ImageProvider imageProvider;
final bool useImage;
final Widget zoomableWidget;
/// While [imageProvider] is not resolved, [loadingChild] is build by [PhotoView]
/// into the screen, by default it is a centered [CircularProgressIndicator]
final Widget loadingChild;
/// Changes the background behind image, defaults to `Colors.black`.
final Color backgroundColor;
/// Defines the minimal size in which the image will be allowed to assume, it
/// is proportional to the original image size. Can be either a double or a
/// [PhotoViewComputedScale]
final dynamic minScale;
/// Defines the maximal size in which the image will be allowed to assume, it
/// is proportional to the original image size. Can be either a double or a
/// [PhotoViewComputedScale]
final dynamic maxScale;
/// This is used to continue showing the old image (`true`), or briefly show
/// nothing (`false`), when the `imageProvider` changes. By default it's set
/// to `false`.
final bool gaplessPlayback;
/// Defines the size of the scaling base of the image inside [PhotoView],
/// by default it is `MediaQuery.of(context).size`. This argument is used by
/// [PhotoViewInline] class.
final Size size;
/// Assists the activation of a hero animation within [PhotoView]
final Object heroTag;
final PhotoViewScaleStateChangedCallback scaleStateChangedCallback;
@override
State<StatefulWidget> createState() {
return _PhotoViewState();
}
}
class _PhotoViewState extends State<PhotoView> {
PhotoViewScaleState _scaleState;
ImageInfo _imageInfo;
// Future<ImageInfo> _getImage() {
// final Completer completer = Completer<ImageInfo>();
// final ImageStream stream =
// widget.imageProvider.resolve(const ImageConfiguration());
// final listener = (ImageInfo info, bool synchronousCall) {
// if (!completer.isCompleted) {
// completer.complete(info);
// setState(() {
// _imageInfo = info;
// });
// }
// };
// stream.addListener(listener);
// completer.future.then((_) {
// stream.removeListener(listener);
// });
// return completer.future;
// }
void setNextScaleState(PhotoViewScaleState newScaleState) {
setState(() {
_scaleState = newScaleState;
});
widget.scaleStateChangedCallback != null
? widget.scaleStateChangedCallback(newScaleState)
: null;
}
void onStartPanning() {
setState(() {
_scaleState = PhotoViewScaleState.zooming;
});
widget.scaleStateChangedCallback != null
? widget.scaleStateChangedCallback(PhotoViewScaleState.zooming)
: null;
}
@override
void initState() {
super.initState();
// if (widget.useImage)
//_getImage();
_scaleState = PhotoViewScaleState.contained;
}
@override
Widget build(BuildContext context) {
// return widget.heroTag == null || !widget.useImage
// ? buildWithFuture(context)
// : buildSync(context);
return buildSync(context);
}
// Widget buildWithFuture(BuildContext context) {
// return FutureBuilder(
// future: _getImage(),
// builder: (BuildContext context, AsyncSnapshot<ImageInfo> info) {
// if (info.hasData) {
// return buildWrapper(context, info.data);
// } else {
// return buildLoading();
// }
// });
// }
Widget buildSync(BuildContext context) {
// if (_imageInfo == null) {
// return buildLoading();
// }
return buildWrapper(context, widget.useImage ? _imageInfo : null);
}
Widget buildWrapper(BuildContext context, [ImageInfo info]) {
return PhotoViewImageWrapper(
setNextScaleState: setNextScaleState,
onStartPanning: onStartPanning,
imageProvider: widget.imageProvider,
imageInfo: info,
scaleState: _scaleState,
backgroundColor: widget.backgroundColor,
gaplessPlayback: widget.gaplessPlayback,
size: widget.size ?? MediaQuery.of(context).size,
scaleBoundaries: ScaleBoundaries(
widget.minScale ?? 0.0,
widget.maxScale ?? 100000000000.0,
imageInfo: info,
size: widget.size ?? MediaQuery.of(context).size,
),
heroTag: widget.heroTag,
useImage: widget.useImage,
zoomableWidget: widget.zoomableWidget,
);
}
Widget buildLoading() {
return widget.loadingChild != null
? widget.loadingChild
: Center(
child: Container(
width: 20.0,
height: 20.0,
child: const CircularProgressIndicator(),
),
);
}
}
/// A [StatelessWidget] which the only child is a [PhotoView] with an automacally
/// calculated [size]. All but [size] arguments are the same as [PhotoView].
class PhotoViewInline extends StatefulWidget {
final ImageProvider imageProvider;
final Widget loadingChild;
final Color backgroundColor;
final dynamic minScale;
final dynamic maxScale;
final bool gaplessPlayback;
final Object heroTag;
final PhotoViewScaleStateChangedCallback scaleStateChangedCallback;
final Widget zoomableWidget;
final bool useImage;
const PhotoViewInline(
{Key key,
this.imageProvider,
this.loadingChild,
this.backgroundColor = const Color.fromRGBO(0, 0, 0, 1.0),
this.minScale,
this.maxScale,
this.gaplessPlayback = false,
this.heroTag,
this.scaleStateChangedCallback,
this.useImage = true,
this.zoomableWidget})
: super(key: key);
@override
State<StatefulWidget> createState() => _PhotoViewInlineState();
}
class _PhotoViewInlineState extends State<PhotoViewInline>
with AfterLayoutMixin<PhotoViewInline> {
Size _size;
@override
void afterFirstLayout(BuildContext context) {
setState(() {
_size = context.size;
});
}
@override
Widget build(BuildContext context) {
return PhotoView(
imageProvider: widget.imageProvider,
loadingChild: widget.loadingChild,
backgroundColor: widget.backgroundColor,
minScale: widget.minScale,
maxScale: widget.maxScale,
gaplessPlayback: widget.gaplessPlayback,
size: _size,
heroTag: widget.heroTag,
useImage: widget.useImage,
zoomableWidget: widget.zoomableWidget,
scaleStateChangedCallback: widget.scaleStateChangedCallback,
);
}
}