Fix hero behavior

This commit is contained in:
Vishnu Mohandas 2020-06-11 03:06:01 +05:30
parent 25de85aea6
commit 83fc76f479
3 changed files with 24 additions and 18 deletions

View file

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

View file

@ -129,12 +129,18 @@ class _GalleryState extends State<Gallery> {
}
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(
onTap: () {
if (_selectedPhotos.isNotEmpty) {
_selectPhoto(photo);
} else {
routeToDetailPage(photo, context);
_routeToDetailPage(photo, context);
}
},
onLongPress: () {
@ -148,9 +154,7 @@ class _GalleryState extends State<Gallery> {
? Border.all(width: 4.0, color: Colors.blue)
: 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(
_photos,
_photos.indexOf(photo),

View file

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