From 3a37f1a35f2c06def9bd2c2ab025e8fc53a58db1 Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Fri, 24 Apr 2020 23:04:46 +0530 Subject: [PATCH] Extract zoomable image into a separate file --- lib/ui/detail_page.dart | 52 ++-------------------------- lib/ui/zoomable_image.dart | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 50 deletions(-) create mode 100644 lib/ui/zoomable_image.dart diff --git a/lib/ui/detail_page.dart b/lib/ui/detail_page.dart index 997526404..eada570b7 100644 --- a/lib/ui/detail_page.dart +++ b/lib/ui/detail_page.dart @@ -1,12 +1,9 @@ -import 'dart:typed_data'; - import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; import 'package:myapp/core/lru_map.dart'; import 'package:myapp/models/photo.dart'; -import 'package:photo_view/photo_view.dart'; +import 'package:myapp/ui/zoomable_image.dart'; import 'extents_page_view.dart'; -import 'loading_widget.dart'; import 'package:myapp/utils/share_util.dart'; class DetailPage extends StatefulWidget { @@ -40,7 +37,7 @@ class _DetailPageState extends State { return _cachedImages.get(index); } final image = ZoomableImage( - photo: widget.photos[index], + widget.photos[index], shouldDisableScroll: (value) { setState(() { _shouldDisableScroll = value; @@ -77,48 +74,3 @@ class _DetailPageState extends State { ); } } - -class ZoomableImage extends StatelessWidget { - final Function(bool) shouldDisableScroll; - - const ZoomableImage({ - Key key, - @required this.photo, - this.shouldDisableScroll, - }) : super(key: key); - - final Photo photo; - - @override - Widget build(BuildContext context) { - Logger().i("Building " + photo.toString()); - if (ImageLruCache.getData(photo.generatedId) != null) { - return _buildPhotoView(ImageLruCache.getData(photo.generatedId)); - } - return FutureBuilder( - future: photo.getBytes(), - builder: (_, snapshot) { - if (snapshot.hasData) { - return _buildPhotoView(snapshot.data); - } else if (snapshot.hasError) { - return Text(snapshot.error.toString()); - } else { - return loadWidget; - } - }, - ); - } - - Widget _buildPhotoView(Uint8List imageData) { - ValueChanged scaleStateChangedCallback = (value) { - if (shouldDisableScroll != null) { - shouldDisableScroll(value != PhotoViewScaleState.initial); - } - }; - return PhotoView( - imageProvider: Image.memory(imageData).image, - scaleStateChangedCallback: scaleStateChangedCallback, - minScale: PhotoViewComputedScale.contained, - ); - } -} diff --git a/lib/ui/zoomable_image.dart b/lib/ui/zoomable_image.dart new file mode 100644 index 000000000..e2ed656b9 --- /dev/null +++ b/lib/ui/zoomable_image.dart @@ -0,0 +1,70 @@ +import 'dart:typed_data'; + +import 'package:flutter/widgets.dart'; +import 'package:logger/logger.dart'; +import 'package:myapp/core/lru_map.dart'; +import 'package:myapp/models/photo.dart'; +import 'package:myapp/ui/image_widget.dart'; +import 'package:myapp/ui/loading_widget.dart'; +import 'package:photo_view/photo_view.dart'; + +class WidgetLruCache { + static LRUMap _map = LRUMap(500); + + static Widget get(Photo photo) { + return _map.get(photo.generatedId); + } + + static void put(Photo photo, Widget data) { + _map.put(photo.generatedId, data); + } +} + +class ZoomableImage extends StatelessWidget { + final Function(bool) shouldDisableScroll; + + const ZoomableImage( + this.photo, { + Key key, + this.shouldDisableScroll, + }) : super(key: key); + + final Photo photo; + + @override + Widget build(BuildContext context) { + Logger().i("Building " + photo.toString()); + if (WidgetLruCache.get(photo) != null) { + return WidgetLruCache.get(photo); + } + Logger().i("Cache miss " + photo.toString()); + return FutureBuilder( + future: photo.getBytes(), + builder: (_, snapshot) { + if (snapshot.hasData) { + final photoView = _buildPhotoView(snapshot.data); + WidgetLruCache.put(photo, photoView); + return photoView; + } else if (snapshot.hasError) { + return Text(snapshot.error.toString()); + } else { + Logger().i("Loading"); + return ImageWidget(photo); + } + }, + ); + } + + Widget _buildPhotoView(Uint8List imageData) { + ValueChanged scaleStateChangedCallback = (value) { + if (shouldDisableScroll != null) { + shouldDisableScroll(value != PhotoViewScaleState.initial); + } + }; + return PhotoView( + imageProvider: Image.memory(imageData).image, + scaleStateChangedCallback: scaleStateChangedCallback, + minScale: PhotoViewComputedScale.contained, + ); + } +}