Flutter's InteractiveViewer widget is a versatile tool that allows users to interact with child widgets using gestures like pan, zoom, and scale. It's particularly useful for displaying large images, maps, or any content that users may want to explore in detail. Let's delve into the attributes of the InteractiveViewer widget and provide an example for your blog.
1. Attributes of InteractiveViewer Widget:
1.1 child:
Description: The widget that will be interactive.
Example:
InteractiveViewer( child: Image.network('https://example.com/large_image.jpg'), ),
1.2 boundaryMargin:
Description: Defines the margin around the child where gestures are still active but won't cause a transformation.
Example:
InteractiveViewer( boundaryMargin: EdgeInsets.all(20.0), child: /* your child widget */, ),
1.3 minScale and maxScale:
Description: Limits the minimum and maximum scale factor that can be applied to the child.
Example:
InteractiveViewer( minScale: 0.8, maxScale: 2.5, child: /* your child widget */, ),
1.4 constrained:
Description: If set to true, the child will be constrained to fit within the viewport.
Example:
InteractiveViewer( constrained: true, child: /* your child widget */, ),
2. Example of InteractiveViewer Widget:
Let's create a simple Flutter application that utilizes the InteractiveViewer widget to allow users to zoom and pan a large image.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('InteractiveViewer Example'),
),
body: Center(
child: InteractiveViewer(
boundaryMargin: EdgeInsets.all(20.0),
minScale: 0.5,
maxScale: 2.0,
constrained: true,
child: Image.network(
'https://example.com/large_image.jpg',
width: 800,
height: 600,
fit: BoxFit.cover,
),
),
),
),
);
}
}
In this example, we've created a Flutter app with a single InteractiveViewer widget containing a large image. Users can pan, zoom, and scale the image within the specified constraints.