Updated 21 December 2023
PhotoView enables images to become able to zoom and pan with user gestures such as pinch, rotate, and drag. PhotoView in Flutter is easy to implement. Let’s check it out.
PhotoView is a simple zoomable image/content widget for Flutter. It also can show any widget such as a Container, Text, or SVG.
For more Flutter app development, please check out our Flutter app development page.
Add this package in your pubspec for using PhotoView in Flutter: photo_view
IMPLEMENTATION:
Create a new project & add this dependency:
1 |
photo_view: ^0.14.0 |
Importing:
1 |
import 'package:photo_view/photo_view.dart'; |
Now use the PhotoView widget & add an image in the image provider & you can use other properties according to your need.
1 2 3 4 5 6 7 8 9 10 |
Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: PhotoView(imageProvider: const NetworkImage("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg"),) ); } |
Use the PhotoViewGallery builder to show, rotate & zoom multiple images.
Loading Builder is used to show a focused circular progress indicator that is called by photo view.
Above mentioned properties are used in the gallery.
CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
class _MyHomePageState extends State<MyHomePage> { List<String> images = [ "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg", "https://images.pexels.com/photos/268533/pexels-photo-268533.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500", "https://images.unsplash.com/photo-1552831544-246186664150?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1yZWxhdGVkfDE0fHx8ZW58MHx8fHw%3D&w=1000&q=80" ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: PhotoViewGallery.builder( itemCount: 3, builder: (BuildContext context, int index) { return PhotoViewGalleryPageOptions( imageProvider: NetworkImage(images[index]), initialScale: PhotoViewComputedScale.contained * 0.8, minScale: PhotoViewComputedScale.contained * 0.8, maxScale: PhotoViewComputedScale.covered * 2, heroAttributes: PhotoViewHeroAttributes(tag: index), ); }, scrollPhysics: BouncingScrollPhysics(), loadingBuilder: (context, event) => Center( child: Container( width: 20.0, height: 20.0, child: const CircularProgressIndicator(), ), ), backgroundDecoration: BoxDecoration( color: Theme.of(context).canvasColor, ), ) ); } } |
In this flutter article, I have explained a PhotoView in a flutter, which you can modify and experiment with according to your own, this little introduction was from the PhotoView demo from our side.
Thanks for reading this article.
If I got something wrong, let me know in the comments. I would love to improve.
For more interesting blogs check out here – https://mobikul.com/blog/
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.