From 7cf4cc40f9b3202b94bcdf52abecc03fb0d736e0 Mon Sep 17 00:00:00 2001 From: Theo Bouwman Date: Sun, 15 Oct 2017 16:28:07 +0200 Subject: [PATCH] pinch to zoom route --- lib/image_carousel.dart | 59 ++++++++++++++++++++++++++++++++++++----- pubspec.yaml | 1 + 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/lib/image_carousel.dart b/lib/image_carousel.dart index 30673e0..6a7e41a 100644 --- a/lib/image_carousel.dart +++ b/lib/image_carousel.dart @@ -5,23 +5,31 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'dart:async'; +import 'package:zoomable_image/zoomable_image.dart'; class ImageCarousel extends StatefulWidget { final List images; final double height; final TargetPlatform platform; final Duration interval; + final bool allowZoom; // Images will shrink according to the value of [height] // If you prefer to use the Material or Cupertino style activity indicator set the [platform] parameter // Set [interval] to let the carousel loop through each photo automatically - ImageCarousel(this.images, {this.height = 250.0, this.platform, this.interval}); + // Pinch to zoom will be turned on by default + ImageCarousel(this.images, + {this.height = 250.0, + this.platform, + this.interval, + this.allowZoom = true}); @override State createState() => new _ImageCarouselState(); } -class _ImageCarouselState extends State with SingleTickerProviderStateMixin { +class _ImageCarouselState extends State + with SingleTickerProviderStateMixin { TabController _tabController; @override @@ -32,7 +40,10 @@ class _ImageCarouselState extends State with SingleTickerProvider if (widget.interval != null) { new Timer.periodic(widget.interval, (_) { - _tabController.animateTo(_tabController.index == _tabController.length - 1 ? 0 : ++_tabController.index); + _tabController.animateTo( + _tabController.index == _tabController.length - 1 + ? 0 + : ++_tabController.index); }); } } @@ -107,14 +118,48 @@ class _CarouselImageState extends State { } } + Widget _getIndicator(TargetPlatform platform) { + if (platform == TargetPlatform.iOS) { + return new CupertinoActivityIndicator(); + } else { + return new CircularProgressIndicator(); + } + } + + void _toZoomRoute() { + Widget scaffold = new Scaffold( + body: new Center( + child: new ZoomableImage( + _image.image, + scale: 16.0, + ), + ), + ); + + Navigator.of(context).push( + defaultTargetPlatform == TargetPlatform.iOS + ? new CupertinoPageRoute( + builder: (BuildContext context) => scaffold) + : new MaterialPageRoute( + builder: (BuildContext context) => scaffold), + ); + } + @override Widget build(BuildContext context) { return new Center( child: _loading - ? widget.carousel.platform == TargetPlatform.android - ? new CircularProgressIndicator() - : new CupertinoActivityIndicator() - : _image, + ? _getIndicator(widget.carousel.platform == null + ? defaultTargetPlatform + : widget.carousel.platform) + : new GestureDetector( + child: _image, + onTap: () { + if (widget.carousel.allowZoom) { + _toZoomRoute(); + } + }, + ), ); } } diff --git a/pubspec.yaml b/pubspec.yaml index 2085b23..21ac541 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,6 +5,7 @@ description: A image carousel flutter plugin version: 0.1.3 dependencies: + zoomable_image: "^1.1.1" flutter: sdk: flutter