Skip to content

Commit 23063d5

Browse files
authored
added wantKeepAlive parameter to make it optional (#479)
1 parent b0668f4 commit 23063d5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/photo_view.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class PhotoView extends StatefulWidget {
237237
required this.imageProvider,
238238
this.loadingBuilder,
239239
this.backgroundDecoration,
240+
this.wantKeepAlive = false,
240241
this.gaplessPlayback = false,
241242
this.heroAttributes,
242243
this.scaleStateChangedCallback,
@@ -273,6 +274,7 @@ class PhotoView extends StatefulWidget {
273274
required this.child,
274275
this.childSize,
275276
this.backgroundDecoration,
277+
this.wantKeepAlive = false,
276278
this.heroAttributes,
277279
this.scaleStateChangedCallback,
278280
this.enableRotation = false,
@@ -312,6 +314,11 @@ class PhotoView extends StatefulWidget {
312314
/// Changes the background behind image, defaults to `Colors.black`.
313315
final BoxDecoration? backgroundDecoration;
314316

317+
/// This is used to keep the state of an image in the gallery (e.g. scale state).
318+
/// `false` -> resets the state (default)
319+
/// `true` -> keeps the state
320+
final bool wantKeepAlive;
321+
315322
/// This is used to continue showing the old image (`true`), or briefly show
316323
/// nothing (`false`), when the `imageProvider` changes. By default it's set
317324
/// to `false`.
@@ -545,7 +552,7 @@ class _PhotoViewState extends State<PhotoView>
545552
}
546553

547554
@override
548-
bool get wantKeepAlive => true;
555+
bool get wantKeepAlive => widget.wantKeepAlive;
549556
}
550557

551558
/// The default [ScaleStateCycle]

lib/photo_view_gallery.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class PhotoViewGallery extends StatefulWidget {
106106
required this.pageOptions,
107107
this.loadingBuilder,
108108
this.backgroundDecoration,
109+
this.wantKeepAlive = false,
109110
this.gaplessPlayback = false,
110111
this.reverse = false,
111112
this.pageController,
@@ -129,6 +130,7 @@ class PhotoViewGallery extends StatefulWidget {
129130
required this.builder,
130131
this.loadingBuilder,
131132
this.backgroundDecoration,
133+
this.wantKeepAlive = false,
132134
this.gaplessPlayback = false,
133135
this.reverse = false,
134136
this.pageController,
@@ -162,6 +164,9 @@ class PhotoViewGallery extends StatefulWidget {
162164
/// Mirror to [PhotoView.backgroundDecoration]
163165
final BoxDecoration? backgroundDecoration;
164166

167+
/// Mirror to [PhotoView.wantKeepAlive]
168+
final bool wantKeepAlive;
169+
165170
/// Mirror to [PhotoView.gaplessPlayback]
166171
final bool gaplessPlayback;
167172

@@ -246,6 +251,7 @@ class _PhotoViewGalleryState extends State<PhotoViewGallery> {
246251
child: pageOption.child,
247252
childSize: pageOption.childSize,
248253
backgroundDecoration: widget.backgroundDecoration,
254+
wantKeepAlive: widget.wantKeepAlive,
249255
controller: pageOption.controller,
250256
scaleStateController: pageOption.scaleStateController,
251257
customSize: widget.customSize,
@@ -270,6 +276,7 @@ class _PhotoViewGalleryState extends State<PhotoViewGallery> {
270276
imageProvider: pageOption.imageProvider,
271277
loadingBuilder: widget.loadingBuilder,
272278
backgroundDecoration: widget.backgroundDecoration,
279+
wantKeepAlive: widget.wantKeepAlive,
273280
controller: pageOption.controller,
274281
scaleStateController: pageOption.scaleStateController,
275282
customSize: widget.customSize,

0 commit comments

Comments
 (0)