diff --git a/lib/util/photo_preview/photo_preview.dart b/lib/util/photo_preview/photo_preview.dart index 4e4d6f8..cf7f496 100644 --- a/lib/util/photo_preview/photo_preview.dart +++ b/lib/util/photo_preview/photo_preview.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:loading_indicator/loading_indicator.dart'; @@ -12,54 +13,41 @@ class PhotoPreview extends StatefulWidget { int index; String heroTagSuffix; - PhotoPreview( - {Key key, - this.previewImageDatas, - this.index = 0, - this.heroTagSuffix = ""}) - : super(key: key); - - PhotoPreview._showPhotoViewByData( - {Key key, - this.previewImageDatas, - this.index = 0, - this.heroTagSuffix = ""}) - : super(key: key) { - } + PhotoPreview({Key key, this.previewImageDatas, this.index = 0, this.heroTagSuffix = ""}) : super(key: key); + + PhotoPreview._showPhotoViewByData({Key key, this.previewImageDatas, this.index = 0, this.heroTagSuffix = ""}) : super(key: key) {} - PhotoPreview._showPhotoViewByImages( - {Key key, List images, this.index = 0, this.heroTagSuffix = ""}) - : super(key: key) { + PhotoPreview._showPhotoViewByImages({Key key, List images, this.index = 0, this.heroTagSuffix = ""}) : super(key: key) { if (images != null) { previewImageDatas = List(); for (var img in images) { - previewImageDatas.add(PreviewImageData( - previewImageType: PreviewImageType.netUrl, data: img)); + previewImageDatas.add(PreviewImageData(previewImageType: PreviewImageType.netUrl, data: img)); } } } ///显示类型的一些预览 - static showPhotoPreview( - BuildContext context, List previewImageDatas, - {int currentIndex = 0, String heroTagSuffix}) { - Navigator.of(context).push(new FadeRoute( - page: PhotoPreview._showPhotoViewByData( - previewImageDatas: previewImageDatas, - index: currentIndex, - heroTagSuffix: heroTagSuffix, - ))); + static showPhotoPreview(BuildContext context, List previewImageDatas, {int currentIndex = 0, String heroTagSuffix}) { + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => PhotoPreview._showPhotoViewByData( + previewImageDatas: previewImageDatas, + index: currentIndex, + heroTagSuffix: heroTagSuffix, + ))); } ///显示仅仅是一组list类型的图片预览 - static showPhotoPreviewByimages(BuildContext context, List images, - {int currentIndex = 0, String heroTagSuffix = ""}) { - Navigator.of(context).push(new FadeRoute( - page: PhotoPreview._showPhotoViewByImages( - images: images, - index: currentIndex, - heroTagSuffix: heroTagSuffix, - ))); + static showPhotoPreviewByimages(BuildContext context, List images, {int currentIndex = 0, String heroTagSuffix = ""}) { + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => PhotoPreview._showPhotoViewByImages( + images: images, + index: currentIndex, + heroTagSuffix: heroTagSuffix, + ))); } @override @@ -67,24 +55,23 @@ class PhotoPreview extends StatefulWidget { } class _PhotoPreviewState extends State { - PageController pageController; - @override void initState() { - pageController = PageController(keepPage: true, initialPage: widget.index); super.initState(); } @override Widget build(BuildContext context) { - return Scaffold( - backgroundColor: Colors.transparent, - body: PhotoViewGalleryScreen( + return PhotoViewGalleryScreen( images: widget.previewImageDatas, index: widget.index, - controller: pageController, heroTagSuffix: widget.heroTagSuffix ?? "", - )); + ); + } + + @override + void dispose() { + super.dispose(); } } @@ -95,8 +82,7 @@ class PreviewImageData { bool isSelect; String tag; - PreviewImageData( - {this.previewImageType, this.data, this.isSelect = false, this.tag}); + PreviewImageData({this.previewImageType, this.data, this.isSelect = false, this.tag}); } ///预览类型 @@ -113,35 +99,38 @@ class PhotoViewGalleryScreen extends StatefulWidget { List images = []; int index = 0; String heroTagSuffix; - PageController controller; - - PhotoViewGalleryScreen( - {Key key, - @required this.images, - this.index, - this.controller, - this.heroTagSuffix}) - : super(key: key) { - controller = PageController(initialPage: index); - } + + PhotoViewGalleryScreen({Key key, @required this.images, this.index, this.heroTagSuffix}) : super(key: key); @override _PhotoViewGalleryScreenState createState() => _PhotoViewGalleryScreenState(); } -class _PhotoViewGalleryScreenState extends State { +class _PhotoViewGalleryScreenState extends State with AutomaticKeepAliveClientMixin { int currentIndex = 0; + PageController pageController; + PhotoViewScaleStateController scaleStateController; @override void initState() { + scaleStateController = PhotoViewScaleStateController(); // TODO: implement initState - super.initState(); + pageController = PageController(initialPage: widget?.index ?? 0); currentIndex = widget.index; + super.initState(); + } + + @override + void dispose() { + scaleStateController.dispose(); + pageController.dispose(); + super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( + backgroundColor: Colors.black, body: Stack( children: [ GestureDetector( @@ -149,45 +138,45 @@ class _PhotoViewGalleryScreenState extends State { Navigator.pop(context); }, child: Container( - color: Colors.black, - child: PhotoViewGallery.builder( - scrollPhysics: const BouncingScrollPhysics(), - builder: (BuildContext context, int index) { - return PhotoViewGalleryPageOptions( - imageProvider: _buildPage(context, index), - heroAttributes: widget.heroTagSuffix.isNotEmpty - ? PhotoViewHeroAttributes( - tag: (widget.images[index].data ?? "") + - widget.heroTagSuffix) - : null, - ); - }, - itemCount: widget.images.length, - loadFailedChild: Container( - child: Text("加载失败"), - ), - loadingBuilder: (context,event){ - return Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Container( + margin: EdgeInsets.all(1), + child: PhotoViewGallery.builder( + scrollPhysics: const BouncingScrollPhysics(), + builder: (BuildContext context, int index) { + return PhotoViewGalleryPageOptions( + scaleStateController: scaleStateController, + imageProvider: _buildPage(context, index), + heroAttributes: widget.heroTagSuffix.isNotEmpty ? PhotoViewHeroAttributes(tag: (widget.images[index].data ?? "") + widget.heroTagSuffix) : null, + ); + }, + itemCount: widget.images.length, + loadFailedChild: Container( + child: Text("加载失败"), + ), + loadingBuilder: (context, event) { + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Container( width: 48, - constraints: BoxConstraints(maxWidth: 48,maxHeight: 48), + constraints: BoxConstraints(maxWidth: 48, maxHeight: 48), height: 48, - child: LoadingIndicator(indicatorType: Indicator.ballSpinFadeLoader,color: Colors.white,) - ), - ], - ); - }, - backgroundDecoration: BoxDecoration(color: Colors.black), - pageController: widget.controller, - enableRotation: true, - onPageChanged: (index) { - setState(() { - currentIndex = index; - }); - }, - )), + child: LoadingIndicator( + indicatorType: Indicator.ballSpinFadeLoader, + color: Colors.white, + )), + ], + ); + }, + backgroundDecoration: BoxDecoration(color: Colors.black), + pageController: pageController, + enableRotation: true, + onPageChanged: (index) { + setState(() { + currentIndex = index; + }); + }, + ), + ), ), Positioned( //图片index显示 @@ -195,8 +184,7 @@ class _PhotoViewGalleryScreenState extends State { width: MediaQuery.of(context).size.width, child: Container( child: Center( - child: Text("${currentIndex + 1}/${widget.images.length}", - style: TextStyle(color: Colors.white, fontSize: 16)), + child: Text("${currentIndex + 1}/${widget.images.length}", style: TextStyle(color: Colors.white, fontSize: 16)), ), ), ), @@ -228,6 +216,10 @@ class _PhotoViewGalleryScreenState extends State { return Image.file(File(item.data)).image; } } + + @override + // TODO: implement wantKeepAlive + bool get wantKeepAlive => true; } class FadeRoute extends PageRouteBuilder {