Flutter交互式画廊插件interactive_gallery的使用
Flutter交互式画廊插件interactive_gallery的使用
平台支持
- ✅ Android
- ✅ iOS
- ✅ MacOS
- ✅ Web
- ✅ Linux
- ✅ Windows
在桌面应用中,由于缺乏滑动功能,可能会显示有限的功能。
开始使用

该包依赖于share_plus
插件来分享你的图片。
使用方法
首先,你需要导入这个库:
import 'package:interactive_gallery/interactive_gallery.dart';
然后,你可以通过传递一个图片列表来调用该组件。图片列表可以是本地文件列表(List<File>
)或网络图片列表(List<String>
)。
InteractiveGallery(imageList: networkImages)
额外信息
以下是该包的一些参数:
imageList
: 图片数据,必需参数,类型为List<File>
(本地图片)或List<String>
(网络图片)。index
: 初始图片索引,默认值为0。minScale
: 最小缩放比例,默认值为1。maxScale
: 最大缩放比例,默认值为5。minStiffness
: 最小滑动刚度,默认值为0.1。maxStiffness
: 最大滑动刚度,默认值为1.5。heightWindowPop
: 当向上或向下滑动时,导航器弹出窗口的高度比例,默认值为0.6。backgroundColor
: 空白区域的颜色,默认值为黑色。firstBottomsheetColor
: 默认底部面板的颜色,默认值为黑色,透明度为0.3。firstBottomsheetHeight
: 默认底部面板的高度,默认值为100。firstBottomsheetBorderRadius
: 默认底部面板的圆角半径,默认值为0。miniatureWidth
: 底部面板中图片的小图标的宽度。singleTapBottomsheetWidget
: 单击屏幕时自定义的底部面板小部件,默认为ImageScroller
小部件,用于显示缩略图。longTapBottomsheetWidget
: 长按屏幕或点击更多选项时自定义的底部面板小部件,默认为ModalSheet
小部件,用于分享图片到其他应用。
自定义底部面板小部件
要自定义底部面板小部件,你可以这样设置:
InteractiveGallery(
imageList: networkImages,
singleTapBottomsheWidget: Container(height: 100, color: Colors.red),
longTapBottomsheetWidget: Container(height: 100, color: Colors.yellow),
)
完整示例代码
以下是一个完整的示例代码,展示了如何使用interactive_gallery
插件。
import 'package:flutter/material.dart';
import 'package:interactive_gallery/interactive_gallery.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(home: Example());
}
}
class Example extends StatefulWidget {
const Example({super.key});
[@override](/user/override)
State<Example> createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
List<String> networkImages = [
'https://www.travelandleisure.com/thmb/aFSOhQj2_rSqHdYLFflyxrTAsW4=/1500x0/filters:no_upscale():max_bytes(150000):strip_icc()/machu-PICCHU0916-2000-c0b8a30f2ce949dc90aff1ef34b7c631.jpg',
'https://i.ytimg.com/vi/u9AMJGOF26g/maxresdefault.jpg',
'https://assets.newatlas.com/dims4/default/e35c3e6/2147483647/strip/true/crop/1600x1669+0+0/resize/1600x1669!/quality/90/?url=http%3A%2F%2Fnewatlas-brightspot.s3.amazonaws.com%2Fff%2Fe6%2F2071913045559fb5f77834f34b4b%2Fcarina.png'
];
[@override](/user/override)
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context, rootNavigator: true).push(PageRouteBuilder(
opaque: false,
pageBuilder: (context, _, __) {
return InteractiveGallery(
imageList: networkImages,
backgroundColor: Colors.black.withOpacity(0.95),
/// CUSTOM SINGLE TAP BOTTOMSHEET, REPLACES THE DEFAULT MINIATURES
// singleTapBottomsheetWidget: Container(height: 100, color: Colors.red),
/// CUSTOM LONG TAP BOTTOMSHEET, REPLACES THE DEFAULT SHARING
// longTapBottomsheetWidget: Container(height: 100, color: Colors.yellow),
);
},
));
},
child: const Text('Navigator'),
),
),
),
);
}
}
更多关于Flutter交互式画廊插件interactive_gallery的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter交互式画廊插件interactive_gallery的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
interactive_gallery
是一个用于 Flutter 的交互式画廊插件,允许用户以交互方式浏览图片、视频或其他内容。它提供了平滑的过渡效果、缩放、拖拽等交互功能,适用于创建图片画廊、视频播放器等场景。
以下是如何使用 interactive_gallery
插件的步骤:
1. 添加依赖
在 pubspec.yaml
文件中添加 interactive_gallery
依赖:
dependencies:
flutter:
sdk: flutter
interactive_gallery: ^1.0.0 # 请检查最新版本
然后运行 flutter pub get
以安装依赖。
2. 基本用法
以下是一个简单的示例,展示如何使用 InteractiveGallery
创建一个图片画廊:
import 'package:flutter/material.dart';
import 'package:interactive_gallery/interactive_gallery.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: GalleryScreen(),
);
}
}
class GalleryScreen extends StatelessWidget {
final List<String> imageUrls = [
'https://via.placeholder.com/400',
'https://via.placeholder.com/500',
'https://via.placeholder.com/600',
'https://via.placeholder.com/700',
];
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Interactive Gallery'),
),
body: InteractiveGallery(
items: imageUrls.map((url) => GalleryItem(url: url)).toList(),
),
);
}
}
3. 参数说明
InteractiveGallery
支持以下参数:
items
: 必填,List<GalleryItem>
,表示画廊中的内容。GalleryItem
可以包含图片、视频或其他自定义内容。initialIndex
: 初始显示的索引,默认为0
。onPageChanged
: 当页面切换时的回调函数。controller
: 用于控制画廊的InteractiveGalleryController
。scaleEnabled
: 是否启用缩放功能,默认为true
。dragEnabled
: 是否启用拖拽功能,默认为true
。
4. 高级用法
自定义内容
GalleryItem
支持自定义内容,例如视频或复杂布局:
InteractiveGallery(
items: [
GalleryItem(
child: VideoPlayerWidget(videoUrl: 'https://example.com/video.mp4'),
),
GalleryItem(
child: Container(
color: Colors.blue,
child: Center(child: Text('Custom Content')),
),
),
],
)
控制画廊
使用 InteractiveGalleryController
控制画廊的行为,例如跳转到指定页面:
final controller = InteractiveGalleryController();
InteractiveGallery(
controller: controller,
items: imageUrls.map((url) => GalleryItem(url: url)).toList(),
);
// 跳转到第二页
controller.jumpToPage(1);
5. 注意事项
- 确保图片或视频的 URL 可访问。
- 如果需要加载网络图片,可以使用
Image.network
或CachedNetworkImage
。 - 如果需要支持视频,可以结合
video_player
插件。
6. 完整示例
以下是一个完整的示例,展示如何创建一个包含图片和视频的交互式画廊:
import 'package:flutter/material.dart';
import 'package:interactive_gallery/interactive_gallery.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: GalleryScreen(),
);
}
}
class GalleryScreen extends StatelessWidget {
final List<GalleryItem> items = [
GalleryItem(url: 'https://via.placeholder.com/400'),
GalleryItem(url: 'https://via.placeholder.com/500'),
GalleryItem(
child: VideoPlayerWidget(videoUrl: 'https://www.example.com/sample.mp4'),
),
];
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Interactive Gallery'),
),
body: InteractiveGallery(
items: items,
),
);
}
}
class VideoPlayerWidget extends StatefulWidget {
final String videoUrl;
VideoPlayerWidget({required this.videoUrl});
[@override](/user/override)
_VideoPlayerWidgetState createState() => _VideoPlayerWidgetState();
}
class _VideoPlayerWidgetState extends State<VideoPlayerWidget> {
late VideoPlayerController _controller;
[@override](/user/override)
void initState() {
super.initState();
_controller = VideoPlayerController.network(widget.videoUrl)
..initialize().then((_) {
setState(() {});
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return _controller.value.isInitialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
)
: Center(child: CircularProgressIndicator());
}
[@override](/user/override)
void dispose() {
_controller.dispose();
super.dispose();
}
}