Flutter - Zoom In and Zoom Out an Image
Last Updated :
24 Apr, 2025
In this article we are using the InteractiveViewer widget in Flutter allows you to create interactive and zoomable widgets. It's commonly used for displaying images, maps, or any content that the user can zoom, and interact with. In this article, we are going to implement the InteractiveViewer widget. A sample video is given below to get an idea about what we are going to do in this article.
Basic Syntax of InteractiveViewer Widget
InteractiveViewer(
child: YourChildWidget(),
boundaryMargin: EdgeInsets.all(0.0), // Optional: Margin around the content
minScale: 1.0, // Optional: Minimum scale (zoom out)
maxScale: 3.0, // Optional: Maximum scale (zoom in)
scaleEnabled: true, // Optional: Allow scaling (zooming)
panEnabled: true, // Optional: Allow panning (dragging)
alignPanAxis: false, // Optional: Align panning to one axis
)
Required Tools
To build this app, you need the following items installed on your machine:
- Visual Studio Code / Android Studio
- Android Emulator / iOS Simulator / Physical Device device.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the Package
First of all import material.dart file.
import 'package:flutter/material.dart';
Step 3: Execute the main Method
Here the execution of our app starts.
Dart
void main() => runApp(MyApp());
Step 4: Create MyApp Class
In this class we are going to implement the MaterialApp, here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
// Set the app's primary theme color
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false,
home: InteractiveViewerExample(),
);
}
}
Step 5: Create InteractiveViewerExample Class
In this class we are going to Implement the InteractiveViewer widget. Here we use the InteractiveViewer widget to wrap an Image.network widget. Here We set various properties of InteractiveViewer:
- boundaryMargin: Defines the margin around the content within the InteractiveViewer.
- minScale: Specifies the minimum scale (zoom out).
- maxScale: Specifies the maximum scale (zoom in).
Comments are added for better understanding.
InteractiveViewer(
boundaryMargin: EdgeInsets.all(20.0), // Margin around the content
minScale: 0.5, // Minimum scale (zoom out)
maxScale: 2.0, // Maximum scale (zoom in)
child: Image.network(
'https://yt3.googleusercontent.com/ytc/APkrFKZjMrx4Pkr0_ZgCnLe2kDYI8K332WXs6lc2yIk5Zw=s900-c-k-c0x00ffffff-no-rj',
width: 300.0, // Width of the initial image
height: 300.0, // Height of the initial image
fit: BoxFit.cover, // BoxFit to fill the available space
),
),
Dart
class InteractiveViewerExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('InteractiveViewer Example'),
),
body: Center(
child: InteractiveViewer(
boundaryMargin: EdgeInsets.all(20.0), // Margin around the content
minScale: 0.5, // Minimum scale (zoom out)
maxScale: 2.0, // Maximum scale (zoom in)
child: Image.network(
'https://yt3.googleusercontent.com/ytc/APkrFKZjMrx4Pkr0_ZgCnLe2kDYI8K332WXs6lc2yIk5Zw=s900-c-k-c0x00ffffff-no-rj',
width: 300.0, // Width of the initial image
height: 300.0, // Height of the initial image
fit: BoxFit.cover, // BoxFit to fill the available space
),
),
),
);
}
}
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false,
home: InteractiveViewerExample(),
);
}
}
class InteractiveViewerExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('InteractiveViewer Example'),
),
body: Center(
child: InteractiveViewer(
boundaryMargin: EdgeInsets.all(20.0), // Margin around the content
minScale: 0.5, // Minimum scale (zoom out)
maxScale: 2.0, // Maximum scale (zoom in)
child: Image.network(
'https://yt3.googleusercontent.com/ytc/APkrFKZjMrx4Pkr0_ZgCnLe2kDYI8K332WXs6lc2yIk5Zw=s900-c-k-c0x00ffffff-no-rj',
width: 300.0, // Width of the initial image
height: 300.0, // Height of the initial image
fit: BoxFit.cover, // BoxFit to fill the available space
),
),
),
);
}
}
Output:
Similar Reads
How to Create a Zoomable Image in Flutter?
In Flutter, you can create a zoomable image using the GestureDetector and Transform widgets. The GestureDetector widget can be used to detect pinch gestures on the image, and the Transform widget can be used to apply the zoom transformation to the image. How to Use:Container( child: PhotoView( image
3 min read
Flutter - Animate Image Rotation
In Flutter, Image.assets() or Image.network() Widget is used to display images from locally or from the URL. Images can be locally stored in the program or fetched from a network and can be displayed using the Image Widget. Animations can be applied to Images via many techniques. Hence In this artic
4 min read
Rounded Corner Image in Flutter
Rounded images or avatars are commonly used in many mobile applications, including those built with Flutter. There are several ways to create rounded images in Flutter, some of which include: Using the ClipRRect widget: As I mentioned earlier, the ClipRRect widget can be used to clip an image and cr
3 min read
Flutter - Convert an Image into Base64 String
Base64 encoding is used to convert bytes that have binary or text data into ASCII characters. Encoding prevents the data from getting corrupted when it is transferred or processed through a text-only system. In this article, we will discuss how to convert an image into Base64 String. A sample video
4 min read
Display Network Image in Flutter
Flutter has an Image widget to display different types of images. To display images from the internet, the Image.network()function is used.Syntax:Image.network ('source_URL')Constructor of Image.network:Image Image.network( String src, { Key? key, double scale = 1.0, Widget Function(BuildContext, Wi
3 min read
Flutter - Fade a Widget In and Out
The AnimatedOpacity widget in Flutter allows you to animate the opacity (transparency) of a child widget. You can use it to smoothly fade in or out a widget. In this article, we are going to implement the AnimatedOpacity Widget in Flutter and see the basic syntax of it. A sample video is given below
4 min read
Flutter - Dynamic Image List
Dynamic Image List is the list of Images when we add images Dynamically, In this article, we will see how to add images dynamically to the List of Images. It is something like that we are adding some items to the list. There is no need to add dependency to our project. A sample video is given below
5 min read
Flutter - Set Background Image
In this article, we are going to implement how to set the background image in the body of the scaffold. A sample image is given below to get an idea about what we are going to do in this article. Step By Step ImplementationStep 1: Create a New Project in Android StudioTo set up Flutter Development o
3 min read
Flutter - Load Your Image Assets Faster
In Flutter, images play a crucial role in creating visually appealing and engaging user interfaces. However, loading images from the network or assets can sometimes lead to slow performance and hinder the user experience. To tackle this issue, Flutter provides a convenient method called precacheImag
4 min read
Page View Animation in Flutter
Page View is a list that works page by page. In this article, we will gonna do How to Animate the Page when sliding. A sample video is given below to get an idea about what we are going to do in this article. We will use the Transform widget to animate the page. Syntax Creating a Page controller th
4 min read