From ad653e8730ec3c39b718dc262452b9cb58a26ae4 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Mon, 9 Mar 2020 19:22:06 +0900 Subject: [PATCH] fixed hero tags to animate only between a collection lens and an item from that lens --- lib/model/collection_lens.dart | 2 + lib/widgets/album/collection_section.dart | 1 + lib/widgets/album/thumbnail.dart | 57 ++++++++++++----------- lib/widgets/fullscreen/image_page.dart | 2 +- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/model/collection_lens.dart b/lib/model/collection_lens.dart index 376141158..862cdb416 100644 --- a/lib/model/collection_lens.dart +++ b/lib/model/collection_lens.dart @@ -64,6 +64,8 @@ class CollectionLens with ChangeNotifier { List get sortedEntries => List.unmodifiable(sections.entries.expand((e) => e.value)); + Object heroTag(ImageEntry entry) => '$hashCode${entry.uri}'; + void sort(SortFactor sortFactor) { this.sortFactor = sortFactor; updateSections(); diff --git a/lib/widgets/album/collection_section.dart b/lib/widgets/album/collection_section.dart index 29dbe87af..d57618c67 100644 --- a/lib/widgets/album/collection_section.dart +++ b/lib/widgets/album/collection_section.dart @@ -44,6 +44,7 @@ class SectionSliver extends StatelessWidget { child: Thumbnail( entry: entry, extent: mqWidth / columnCount, + heroTag: collection.heroTag(entry), ), ); }, diff --git a/lib/widgets/album/thumbnail.dart b/lib/widgets/album/thumbnail.dart index 444763481..1597fa2c7 100644 --- a/lib/widgets/album/thumbnail.dart +++ b/lib/widgets/album/thumbnail.dart @@ -8,6 +8,7 @@ import 'package:flutter/material.dart'; class Thumbnail extends StatelessWidget { final ImageEntry entry; final double extent; + final Object heroTag; static final Color borderColor = Colors.grey.shade700; static const double borderWidth = .5; @@ -16,6 +17,7 @@ class Thumbnail extends StatelessWidget { Key key, @required this.entry, @required this.extent, + this.heroTag, }) : super(key: key); @override @@ -26,33 +28,36 @@ class Thumbnail extends StatelessWidget { width: 50, height: 50, builder: (bytes) { - return Hero( - tag: entry.uri, - flightShuttleBuilder: ( - BuildContext flightContext, - Animation animation, - HeroFlightDirection flightDirection, - BuildContext fromHeroContext, - BuildContext toHeroContext, - ) { - // use LayoutBuilder only during hero animation - return LayoutBuilder(builder: (context, constraints) { - final dim = min(constraints.maxWidth, constraints.maxHeight); - return Image.memory( - bytes, - width: dim, - height: dim, - fit: BoxFit.cover, - ); - }); - }, - child: Image.memory( - bytes, - width: extent, - height: extent, - fit: BoxFit.cover, - ), + final image = Image.memory( + bytes, + width: extent, + height: extent, + fit: BoxFit.cover, ); + return heroTag == null + ? image + : Hero( + tag: heroTag, + flightShuttleBuilder: ( + BuildContext flightContext, + Animation animation, + HeroFlightDirection flightDirection, + BuildContext fromHeroContext, + BuildContext toHeroContext, + ) { + // use LayoutBuilder only during hero animation + return LayoutBuilder(builder: (context, constraints) { + final dim = min(constraints.maxWidth, constraints.maxHeight); + return Image.memory( + bytes, + width: dim, + height: dim, + fit: BoxFit.cover, + ); + }); + }, + child: image, + ); }, ); return Container( diff --git a/lib/widgets/fullscreen/image_page.dart b/lib/widgets/fullscreen/image_page.dart index 8b9d0c311..e2f94409d 100644 --- a/lib/widgets/fullscreen/image_page.dart +++ b/lib/widgets/fullscreen/image_page.dart @@ -80,7 +80,7 @@ class ImagePageState extends State with AutomaticKeepAliveClientMixin ), backgroundDecoration: backgroundDecoration, heroAttributes: PhotoViewHeroAttributes( - tag: entry.uri, + tag: widget.collection.heroTag(entry), transitionOnUserGestures: true, ), scaleStateChangedCallback: scaleStateChangedCallback,