Skip to content

Commit

Permalink
Implement PhotoView library to enable zoom on gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
normade committed Mar 28, 2023
1 parent 84918bf commit ccfe79e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
54 changes: 24 additions & 30 deletions lib/src/widgets/building_details/gallery_carousel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class GalleryCarousel extends StatefulWidget {
const GalleryCarousel({Key? key, required this.galleryImages, required this.imageIndex}) : super(key: key);
Expand Down Expand Up @@ -32,26 +34,31 @@ class _GalleryCarouselState extends State<GalleryCarousel> {
padding: const EdgeInsets.fromLTRB(0, 40, 0, 0),
child:Row(
children: [
IconButton(
icon: const Icon(Icons.cancel, color: Colors.white),
onPressed: () {Navigator.pop(context);}
)],
IconButton(
icon: const Icon(Icons.cancel, color: Colors.white),
onPressed: () {Navigator.pop(context);}
)
],
)
),
Expanded(
child: PageView.builder(
itemCount: widget.galleryImages.length,
pageSnapping: true,
controller: _pageController,
onPageChanged: (page) {
setState(() {
_activePage = page;
});
},
itemBuilder: (context, pagePosition) {
bool active = pagePosition == _activePage;
return slider(widget.galleryImages, pagePosition, active);
}
child: PhotoViewGallery.builder(
scrollPhysics: const BouncingScrollPhysics(),
builder: (BuildContext context, int index) {
return PhotoViewGalleryPageOptions(
imageProvider: NetworkImage(widget.galleryImages[index]),
initialScale: PhotoViewComputedScale.contained * 0.9,
minScale: PhotoViewComputedScale.contained * 0.8,
maxScale: PhotoViewComputedScale.covered * 1.1,
);
},
itemCount: widget.galleryImages.length,
pageController: _pageController,
onPageChanged: (page) {
setState(() {
_activePage = page;
});
},
)
),
Padding(
Expand All @@ -67,19 +74,6 @@ class _GalleryCarouselState extends State<GalleryCarousel> {
}
}

AnimatedContainer slider(images, pagePosition, active) {
double margin = active ? 10 : 20;

return AnimatedContainer(
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOutCubic,
margin: EdgeInsets.all(margin),
decoration: BoxDecoration(
image: DecorationImage(image: NetworkImage(images[pagePosition]))
),
);
}

List<Widget> indicators(imagesLength, currentIndex) {
return List<Widget>.generate(imagesLength, (index) {
return Expanded(
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.3"
photo_view:
dependency: "direct main"
description:
name: photo_view
sha256: "8036802a00bae2a78fc197af8a158e3e2f7b500561ed23b4c458107685e645bb"
url: "https://pub.dev"
source: hosted
version: "0.14.0"
platform:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies:
latlong2: ^0.8.1
flutter_share_me: ^1.4.0
path_provider: ^2.0.11
photo_view: ^0.14.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit ccfe79e

Please sign in to comment.