浏览代码

Fix hero behavior

Vishnu Mohandas 5 年之前
父节点
当前提交
83fc76f479
共有 3 个文件被更改,包括 24 次插入18 次删除
  1. 14 13
      lib/ui/detail_page.dart
  2. 9 5
      lib/ui/gallery.dart
  3. 1 0
      lib/ui/zoomable_image.dart

+ 14 - 13
lib/ui/detail_page.dart

@@ -25,7 +25,6 @@ class _DetailPageState extends State<DetailPage> {
   bool _shouldDisableScroll = false;
   bool _shouldDisableScroll = false;
   List<Photo> _photos;
   List<Photo> _photos;
   int _selectedIndex = 0;
   int _selectedIndex = 0;
-  PageController _pageController;
 
 
   @override
   @override
   void initState() {
   void initState() {
@@ -34,6 +33,12 @@ class _DetailPageState extends State<DetailPage> {
     super.initState();
     super.initState();
   }
   }
 
 
+  @override
+  void dispose() {
+    Bus.instance.fire(PhotoOpenedEvent(null));
+    super.dispose();
+  }
+
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
     _logger.info("Opening " +
     _logger.info("Opening " +
@@ -54,20 +59,16 @@ class _DetailPageState extends State<DetailPage> {
   }
   }
 
 
   Widget _buildPageView() {
   Widget _buildPageView() {
-    _pageController = PageController(initialPage: _selectedIndex);
     return ExtentsPageView.extents(
     return ExtentsPageView.extents(
       itemBuilder: (context, index) {
       itemBuilder: (context, index) {
         final photo = _photos[index];
         final photo = _photos[index];
-        final image = Hero(
-          tag: photo.hashCode,
-          child: ZoomableImage(
-            photo,
-            shouldDisableScroll: (value) {
-              setState(() {
-                _shouldDisableScroll = value;
-              });
-            },
-          ),
+        final image = ZoomableImage(
+          photo,
+          shouldDisableScroll: (value) {
+            setState(() {
+              _shouldDisableScroll = value;
+            });
+          },
         );
         );
         if (index == _selectedIndex) {
         if (index == _selectedIndex) {
           Bus.instance.fire(PhotoOpenedEvent(photo));
           Bus.instance.fire(PhotoOpenedEvent(photo));
@@ -81,7 +82,7 @@ class _DetailPageState extends State<DetailPage> {
       physics: _shouldDisableScroll
       physics: _shouldDisableScroll
           ? NeverScrollableScrollPhysics()
           ? NeverScrollableScrollPhysics()
           : PageScrollPhysics(),
           : PageScrollPhysics(),
-      controller: _pageController,
+      controller: PageController(initialPage: _selectedIndex),
       itemCount: _photos.length,
       itemCount: _photos.length,
       extents: 1,
       extents: 1,
     );
     );

+ 9 - 5
lib/ui/gallery.dart

@@ -129,12 +129,18 @@ class _GalleryState extends State<Gallery> {
   }
   }
 
 
   Widget _buildPhoto(BuildContext context, Photo photo) {
   Widget _buildPhoto(BuildContext context, Photo photo) {
+    Widget thumbnail;
+    if (_openedPhoto == null || _openedPhoto == photo) {
+      thumbnail = Hero(tag: photo.hashCode, child: ThumbnailWidget(photo));
+    } else {
+      thumbnail = ThumbnailWidget(photo);
+    }
     return GestureDetector(
     return GestureDetector(
       onTap: () {
       onTap: () {
         if (_selectedPhotos.isNotEmpty) {
         if (_selectedPhotos.isNotEmpty) {
           _selectPhoto(photo);
           _selectPhoto(photo);
         } else {
         } else {
-          routeToDetailPage(photo, context);
+          _routeToDetailPage(photo, context);
         }
         }
       },
       },
       onLongPress: () {
       onLongPress: () {
@@ -148,9 +154,7 @@ class _GalleryState extends State<Gallery> {
               ? Border.all(width: 4.0, color: Colors.blue)
               ? Border.all(width: 4.0, color: Colors.blue)
               : null,
               : null,
         ),
         ),
-        child: photo == _openedPhoto
-            ? Hero(tag: photo.hashCode, child: ThumbnailWidget(photo))
-            : ThumbnailWidget(photo),
+        child: thumbnail,
       ),
       ),
     );
     );
   }
   }
@@ -166,7 +170,7 @@ class _GalleryState extends State<Gallery> {
     });
     });
   }
   }
 
 
-  void routeToDetailPage(Photo photo, BuildContext context) {
+  void _routeToDetailPage(Photo photo, BuildContext context) {
     final page = DetailPage(
     final page = DetailPage(
       _photos,
       _photos,
       _photos.indexOf(photo),
       _photos.indexOf(photo),

+ 1 - 0
lib/ui/zoomable_image.dart

@@ -99,6 +99,7 @@ class _ZoomableImageState extends State<ZoomableImage>
         scaleStateChangedCallback: _scaleStateChangedCallback,
         scaleStateChangedCallback: _scaleStateChangedCallback,
         minScale: PhotoViewComputedScale.contained,
         minScale: PhotoViewComputedScale.contained,
         gaplessPlayback: true,
         gaplessPlayback: true,
+        heroAttributes: PhotoViewHeroAttributes(tag: widget.photo.hashCode),
       );
       );
     } else {
     } else {
       return loadWidget;
       return loadWidget;